我正在使用以下代码使用表格从文件夹中删除图像。 它运行了好几个月(每月运行一次)。 然后它开始删除表中的图像,我猜比较失败了。
所以我想看的是正在比较的内容(文件名和表文件名)。 我无法让2并排排列,因此我可以对其进行比较。 此外,目录结构似乎已关闭(“。”和“ ..”)。 请参阅底部的打印声明。 如果需要查看输出,请使用 https://www.stampden.com/users/cron1_delete_images.php
// first put all files into an array
$images = scandir('russromei');
// create a new empty array that gonna hold all files from database
$files = [];
// new we select all files from database
// first make your query
$query = 'SELECT substr(value,41) FROM ppbv79_listings_media where value like "http://www.stampden.com/users/russromei/%"';
// create mysqli object
$mysqli = new Mysqli($dbhost,$dbuser,$dbpass,$dbname);
// prepare statement
// even tho this query is not vulnerable to sql injections i'm gonna use prepared statement
$stmt = $mysqli->prepare($query);
$stmt->execute();
$stmt->bind_result($file);
while($stmt->fetch()){ // insert all files into the array we created earlier
$files[] = $file;
}
$stmt->close();
$mysqli->close();
// now you have an array of all files from database and images in folder
// we are gonna go threw each image to check if it exist in the database
$ct=0;
foreach($images as $img)
{
if(!in_array($img,$files)) {
// delete file
$ct=$ct+1;
// unlink('russromei/'.$img);
// print("Delete file [". $img ."]<br><br>");
// print($ct." ".$img." ".$file."<br>");
print_r($images[$ct]." ---> ".$files[$ct]."<br>");
// print_r($files[$ct]."<br>");
}
}
unset($img);