mcmuney Posted February 27, 2007 Share Posted February 27, 2007 I would like to delete all image files that does not exist within a DB table, is there an easy way to do this? Link to comment https://forums.phpfreaks.com/topic/40370-deleting-files/ Share on other sites More sharing options...
papaface Posted February 27, 2007 Share Posted February 27, 2007 unlink("locationoffile"); http://php.net/unlink/ Link to comment https://forums.phpfreaks.com/topic/40370-deleting-files/#findComment-195315 Share on other sites More sharing options...
TRI0N Posted February 27, 2007 Share Posted February 27, 2007 Just a basic routine <?php $path = './'; // path to the directory to read if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if(strrchr($file, '.') == '.png' || strrchr($file, '.') == '.gif' || strrchr($file, '.') == '.jpg') { if(!is_dir($file)){ // Database Connection mysql_connect("$dbhost","$dbuser","$dbpasswd") ; // Database Selection mysql_select_db("$dbname") ; // Find Picture in Database if No Record Delete $result = mysql_query("SELECT image FROM images WHERE (image = '$file')") ; while($row = mysql_fetch_row($result)) { $image = $row[0] ; } } if($image != $file){ echo $file." <<< No Match Found - File Deleted<br>" ; //Uncomment the line below once you have made sure the Query works and presents you a list of images not found. Just a precaution till you are sure its correct. //unlink("$file") ; } } } closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/40370-deleting-files/#findComment-195507 Share on other sites More sharing options...
mcmuney Posted February 28, 2007 Author Share Posted February 28, 2007 The test worked perfectly, but when I remove the // from //unlink, I get an error saying, "No such path or directory", but the test recognizes the files and executes properly. Is something missing on the unlink line? Link to comment https://forums.phpfreaks.com/topic/40370-deleting-files/#findComment-195723 Share on other sites More sharing options...
TRI0N Posted February 28, 2007 Share Posted February 28, 2007 The first one I orginally made so that it would work out of the dir that you have the images in.. This one is made to work out of whatever $path you provide on line 2. <?php $path = './'; // path to the directory to read ( ./ reads the dir this file is in) if ($handle = opendir($path)) { while (false !== ($file = readdir($handle))) { if(strrchr($file, '.') == '.png' || strrchr($file, '.') == '.gif' || strrchr($file, '.') == '.jpg') { if(!is_dir($file)){ // Database Connection mysql_connect("$dbhost","$dbuser","$dbpasswd") ; // Database Selection mysql_select_db("$dbname") ; // Find Picture in Database if No Record Delete $result = mysql_query("SELECT image FROM images WHERE (image = '$file')") ; while($row = mysql_fetch_row($result)) { $image = $row[0] ; } } if($image != $file){ $file2 = $path.'/'.$file ; if($path == './') { echo $file.' <<< No Match Found - File Deleted<br>' ; //unlink('$file') ; }else{ echo $file2.' <<< No Match Found - File Deleted<br>' ; //unlink('$file2') ; } } } } closedir($handle); } ?> Link to comment https://forums.phpfreaks.com/topic/40370-deleting-files/#findComment-195907 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.