emma57573 Posted March 20, 2012 Share Posted March 20, 2012 Hi Im trying to write a script to clean up my image directory which has quite a lot of unused images that have built up over time. In order to do this I am doing the following. First Create a database table called 'image_clean' Then I'm searching through 3 tables and collecting all the image file names and dumping the names in the table 'image_clean' Can do that no problem. So now I have all the images I need in this one table 'image_clean' I now want to go through my directory 'image_uploads' and delete anything thats not in the 'image_clean' table. I know how to delete the files using unlink Im just unsure how to search through the directory file by file and check the file against the database. Im asumming I need to put them in an array. Could anyone give be a clue or two to get me started. I have no problem checking a database against a directory but when its the other way round 'checking a directory against a database I'm lost. What I might do is pop the files to delete in a new database called 'image_delete' so that I can then check the images to delete before I write the unlink script. But I'm just not sure how to pick up each file and compare it to the table. Thanks in advance. Link to comment https://forums.phpfreaks.com/topic/259343-image-clean-up-script-help/ Share on other sites More sharing options...
dragon_sa Posted March 20, 2012 Share Posted March 20, 2012 $image[] = $row['image']; // put images to keep in array $folder = "path/to/images/"; // set folder path to images $files = glob($folder.'*.*') // open folder and get files foreach ($files as $img) { $ext = substr($img, -3); // check the file extension $ext = strtolower($ext); // make it lower case for ease if (in_array($ext, array('jpg','gif,'png')) && !in_array($img, $image)) { check extension and make sure its not an image to keep unlink($folder."/".$img); // remove image } } Link to comment https://forums.phpfreaks.com/topic/259343-image-clean-up-script-help/#findComment-1329481 Share on other sites More sharing options...
emma57573 Posted March 20, 2012 Author Share Posted March 20, 2012 Thanks for that, that makes sen e. I will have a crack at that when I get home and let you know how I get on. Thanks Link to comment https://forums.phpfreaks.com/topic/259343-image-clean-up-script-help/#findComment-1329508 Share on other sites More sharing options...
dragon_sa Posted March 20, 2012 Share Posted March 20, 2012 in the unlink part I put in the $folder."/". you dont actually need that as its in the $img variable, you just need to use the $img variable also will need to explode $img to check in the array so a bit like this instead $image[] = $row['image']; // put images to keep in array $folder = "path/to/images/"; // set folder path to images $files = glob($folder.'*.*'); // open folder and get files foreach ($files as $img) { $parts = explode("/", $img); // separate variable on the forward slashes $part = $parts[3]; // in this example the folder path and the image create 4 variables for $parts so we want the last one which is $parts[3] $ext = substr($part, -3); // check the file extension $ext = strtolower($ext); // make it lower case for ease if (in_array($ext, array('jpg','gif,'png')) && !in_array($part, $image)) { check extension and make sure its not an image to keep unlink($img); // remove image } } Link to comment https://forums.phpfreaks.com/topic/259343-image-clean-up-script-help/#findComment-1329515 Share on other sites More sharing options...
emma57573 Posted March 21, 2012 Author Share Posted March 21, 2012 Thanks for your help, I simplified it a bit as I don't need to check for extensions but it worked a treat. Cleaned up 790 images great was hoping for move though have 219,000 images on site but it seems like most of them are used $row="select url from images_keep where id !=''"; mysql_query($row) or die('Query failed keepimage: ' . mysql_error()); $image[] = $row['url']; // put images to keep in array $folder = "uploadedimages/"; // set folder path to images $files = glob($folder.'*.*'); // open folder and get files foreach ($files as $img) { $ext = strtolower($img); // make it lower case for ease if (!in_array($ext, $image)){ unlink($ext); // remove image } I tested it by inserting it into a table first to inspect the images to delete, too scary to run straight off! But works great thanks Link to comment https://forums.phpfreaks.com/topic/259343-image-clean-up-script-help/#findComment-1329683 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.