Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6995257
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T19:59:54+00:00 2026-05-27T19:59:54+00:00

First of all, i would like to explain my condition right now. I’m using

  • 0

First of all, i would like to explain my condition right now.

I’m using PHP as my programming language.

I have a table named “Produk”. It keeps every product name. Example value “TWC0001” in its id_produk column.

Every product have its own images, and stored in ./images/Produk/ directory.

the problem is, this project has been working about 1 years ago, and when the users delete a product, the product’s images didn’t deleted too. So, it still staying in ./images/Produk/ directory. It means, that file become a garbage right?


Case Example :

in the “Produk” table, column “id_produk” i have 3 rows :

“TWC0001″,”TWC0002″,”TWC0003”.

Of course each of those rows have its own images that stored in ./images/Produk/

Each of those files named :

“TWC0001.jpg”, “TWC0002.jpg”, “TWC0003.jpg”


Case : A user logged in and deleted row “TWC0002”, of course the “TWC0002.jpg” file still exist.

Problem : I want to delete all “.jpg” files that didn’t listed in the “Produk” table anymore.


I’ve been doing this :

//listing all the ".jpg" files
$arrayfiles=scandir("../images/Produk/");

//getting all the product list
$sql="select * from produk";
$produk=mysql_query($sql,$conn) or die("Error : ".mysql_error());

foreach($arrayfiles as $key=>$value)
{
  while($row=mysql_fetch_array($produk,MYSQL_ASSOC))
  {
    ///here is the part i've been confused of.
  }
}

PHP function to delete file is “unlink()”;

Please anybody help me out of this.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T19:59:55+00:00Added an answer on May 27, 2026 at 7:59 pm

    The following code will produce an array with all the images that have no corresponding product record. I’ve left off the unlink command so you can do some reviewing process first.

    $sql = "SELECT * FROM Produk";
    $result = mysql_query($sql);
    $existing_products = array();
    while ($row = mysql_fetch_array($result)) 
      $existing_products[] = $row["id_produk"] . ".jpg";
    
    $existing_images = array();
    foreach(glob("../images/Produk/*.jpg") as $v) 
      $existing_images[] = str_replace("../images/Produk/", "", $v);
    
    $images_to_delete = array_diff($existing_images, $existing_products);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

first of all i would like to say i know its probably an easy
First of all, I'm really new to the MVC Asp.Net ideology. I would like
I would like all my URLs in codeigniter to start with the first URI
I would like to produce a character list of all of the first letters
First I would like to explain why I want to use alternate Blackberry Persistance
first of all, I'm very new to Powershell, and I would like to thank
First of all let me explain that I have searched for at least an
First of all I would like to remark I am new with the concept
at first I would like to try to explain my plan: Sometimes I just
First of all, is this possible? If so: What challenges would I encounter in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.