I have been working on some project and through time it got messed up with images which I tested it, so now I want to make a script which is going to search in articles img tags and find the img name (artiles are stored in mysql with attribute 'text') after scanning the folder where images are stored if they are not in any article included then to delete those images (unused images). Has anyone done this before so I could see an example or any good approach about this case?
-
The trouble in showing you an example of this is that it is a 'niche' case. There probably isn't any resource we can direct you to and there probably isn't anyone who is willing to put up the time to create one. All we can really do is, break down the specific parts of your request and let you put them together yourself. – user1477388 Feb 20 '14 at 17:18
-
@user1477388 yeah you are right, but simple tips will do like if any one has done anything like this before could help me out like telling me what did he use to search for images in a folder or tell me how to compare file names with those found in mysql ... – TooCooL Feb 20 '14 at 17:22
-
1make an array with all the image file names inside your database and scan each folder to see if a file name exists in the array, if don't, delete it – CIRCLE Feb 20 '14 at 17:22
3 Answers
Here's what you'll need to do what you want:
Loop through your directory of files (if they are on the filesystem):
if ($handle = opendir('/path/to/files')) { echo "Directory handle: $handle\n"; echo "Entries:\n"; /* This is the correct way to loop over the directory. */ while (false !== ($entry = readdir($handle))) { echo "$entry\n"; } /* This is the WRONG way to loop over the directory. */ while ($entry = readdir($handle)) { echo "$entry\n"; } closedir($handle); }
Loop through your files (if they are on the database):
Compare file names (obvious once you are looping through your resource).
Delete unused images like so http://www.php.net/unlink

- 20,790
- 32
- 144
- 264
-
Thank you very much this is what I needed to get started, and accepted your answer – TooCooL Feb 20 '14 at 17:27
Approach is simple
Query database and get list of all image URLs - add to an array
Loop through each folder that contains images and make an array of every image on the site/
here is how to find all items that are in one array but not another (may be a better answer more specific to you -
array_Intesect
is what you need.with the new array simply loop through the list and delete the files.
All of the above you can search individually and then string them together.
I would recommend backing everything up before trying!!!!

- 1
- 1

- 22,724
- 2
- 32
- 64
-
If the OP were to do that he'd better hope he'd backed up first because he'd be deleting all of the images that **are** used on the site... I think you meant `array_diff`? – Steven Feb 20 '14 at 17:31
I recently came accross such thing where I wanted to remove unused files that users left behind / change the profile picture but they were stored on the webserver. To fix this I used this :
$images = scandir("uploads", 1);
foreach ($images as $itemlc)
{
$res=mysql_query("SELECT * FROM company WHERE c_logo='$itemlc'");
$count = mysql_num_rows($res);
$res2=mysql_query("SELECT * FROM users WHERE u_logo='$itemlc'");
$count2 = mysql_num_rows($res2);
if($count == 1)
{
echo $itemlc; echo " exists <br><br>";
}
else if ($count2 == 1)
{
echo $itemlc; echo " exists <br><br>";
}
else{ $file_path = 'uploads/'; $src=$file_path.$itemlc; @unlink($src); }
}
Hope this helps if there is someone who needs this!

- 138
- 1
- 9