delete files that are not in mysql database
Posted: Wed Sep 29, 2004 3:28 am
I am trying to compare a directory that contains picture files with a mysql column that contains the url to these picture files and if there are any picture files in that directory that are not in the database i would like them unlinked. So far i can print out the results of both the column and the folder but im not sure where to go next.
the only picture file i dont want to delete is trans.gif
Anyone have an idea?
Code: Select all
<?php
if ($handle = opendir('/home/path/files/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && $file!="trans.gif") {
echo "http://www.url.com/files/$file<br>\n";
}
}
closedir($handle);
}
echo("<br><br> From Mysql<br><br>");
@mysql_connect(localhost, username, pass) or die("ERROR--CAN'T CONNECT TO SERVER");
@mysql_select_db(database) or die("ERROR--CAN'T CONNECT TO DB");
$sql = "SELECT * FROM messages";
$result = mysql_query($sql) or die(mysql_error());
while($row = mysql_fetch_array($result)){
$variable1=$row["image"];
if ($variable1 !="/files/trans.gif" && $variable1!="files/trans.gif")
{
echo("$variable1<br>");
}
}
?>Anyone have an idea?