Jump to content

Deleting Files


mcmuney

Recommended Posts

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.