Jump to content

Image clean up script help


emma57573

Recommended Posts

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
Share on other sites

$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
Share on other sites

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
Share on other sites

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  :o

 

 

$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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.