Im still quite new to programming and PHP but i thought this may be handy for someone. I am posting this because I had some problems with getting this script to run but eventually got it going. It may not be the best way but it works and thats all that mattered to me 😉
my scenario was that I have mysql db that stores lost of tables. I have things such as galleries, news and more that use images so I save the filename into the DB and store my images in a folder called content_images.
what I wanted was for an easy way to go through that folder, check all the relevant tables and then delete any images that should not be there.
I had some problems especially with the comparing of the arrays because I could not get the expected result using the inbuilt php function such as array_diff
so this is my code I have comment throughout. Hope this helps someone 😉
also if anyone has something to comment on making this better or even a total better way of doing then please let me know 🙂
<?php
// This page will go through and make sure
// that all the images in content_images should be there.
// any images found that shouldnt be there will be deleted automatically.
// get your sql connection
include_once "sqlconnect.php";
// Look through the content_images folder
$path = "../content_images/";
$dir_handle = @opendir($path) or die("Unable to open $path");
while ($file = readdir($dir_handle)) {
if($file == "." || $file == ".." || $file == "Thumbs.db" || $file == "index.php" ) continue;
// get all the files and put them into an array
$allimages[] = $file;
// go through and check all the tables that store image paths
$query = "SELECT filename from ws_product_images WHERE `filename` != ''";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
$filename = $row[0];
if($filename == $file){ $locatedimages[] = "$file"; }
}
$query = "SELECT filename from ws_foodmenu_images WHERE `filename` != ''";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
$filename = $row[0];
if($filename == $file){ $locatedimages[] = "$file"; }
}
$query = "SELECT filename from ws_gallery_images WHERE `filename` != ''";
$result = @mysql_query ($query);
while ($row = mysql_fetch_array($result, MYSQL_NUM)){
$filename = $row[0];
if($filename == $file){ $locatedimages[] = "$file"; }
}
}
closedir($dir_handle);
// Show table to end user to see what has been done
echo "<table border=1 cellspacing=2 cellpadding=5><tr><td valign=top>";
echo "TOTAL IMAGE COUNT: ";
// sort our images
sort($allimages);
// count our images
echo count($allimages);
echo "<br><br>";
// begin a loop search each file from the content_images folder
foreach($allimages as $value1){
// now check that file against the array from our databases
foreach($locatedimages as $value2){
// if we have a match then add |match so we know which one has a match
if($value1 == $value2){ $value1 = "$value1|match"; }
}
// now we explode that new value by each row, then fields image and match
$data = $value1;
$pieces = explode("|", $data);
$image=$pieces[0];
$match=$pieces[1];
// if we dont have a match print on the screen and delete the file
if($match == ""){
echo "<b>$image was not found in our database and has been deleted</b><br>";
$imagefile = "../content_images/".$image;
unlink($imagefile);
// if we do then print on the screen
} else {
echo "$image was found in our database<br>";
}
}
mysql_close();
?>