In a nutshell, I need to query a db column containing image multiple file names per field, then compare them. The image file names are stored in the db as a pipe-delineated string. If a file exists in the images directory, but is not in the db query result set, it should be deleted. This doesn't need to happen on a schedule or anything like that.
Here is what I have so far, but I'm not quite there and could use a little help. There are echo() and var_dump() statements in the code for debugging, but the output volume is pretty large. I can post it if anyone thinks it will help. The result I'm getting is that the array_diff() result is not a true reflection of the differences between the db and the directory. It seems every file is listed in the array_diff() result, so I haven't tested any type of actual unlink() statement yet.
<?php
require("conn.php");
if($handle = opendir('images/'))
{
echo("Directory handle: $handle \n");
echo("Files: <br>\n");
$num = 0;
while( false !== ($file = readdir($handle)))
{
if($file != "." && $file != "..")
{
$filenames[] = $file;
echo("$file <br>\n");
echo( $num );
}
$num ++;
}
var_dump( $filenames );
echo("<br><br>");
closedir($handle);
}
$q1 = "select image from listings where (image is not NULL)";
$r1 = mysql_query($q1);
if(mysql_num_rows($r1) > 0)
{
while($a1 = mysql_fetch_array($r1))
{
$imarray[] = $a1[image];
}
$result = array_diff($imarray, $filenames);
}
echo("\$a1= :<br>");
var_dump($a1);
echo("<br><br>");
echo("Result of var_dump array_diff= <br>\n");
var_dump($result);
echo("<BR><BR>result of print_r(\$result)<br><br>");
print_r($result);
?>