gpt4 book ai didi

php - 从已过期的行中删除图像

转载 作者:行者123 更新时间:2023-11-29 11:02:27 25 4
gpt4 key购买 nike

嘿,我有一个 php 代码,用于检查哪些行日期已过期并删除它们,其中一列是通过 ftp 上传到服务器的图像名称,当我删除所有过期记录时,我也想删除图像那是依附于他们的。我读到了 php 上的 unlink 命令,但我不知道如何将它一次应用于所有图像。

我有这段代码,我只是乱七八糟的,因为我真的不知道该怎么做..

     $sql = "DELETE FROM table WHERE date < NOW()";
mysqli_query($conn,$sql);

$file = "public_html/images/".//Here should go the image name;
unlink($file);
if($sql)
{
echo "Records were deleted";
}
?>

谁能告诉我如何删除附加到已删除行的所有图像?

最佳答案

如果您想从数据库的表中删除行,同时从服务器上保存图像的文件夹中删除图像,那么您必须一次处理每个删除候选者,以便您可以从该行中获取文件名来完成文件删除,同时删除该行。

// connect to database

$base_path = 'public_html/images/';
$del_count = 0;

// SELECT all the delete candidates
$sql = "SELECT id,image FROM table WHERE date < NOW()";
$sel4del = mysqli_query($conn,$sql);
if ( ! $sel4del ) {
echo $sel4del->error;
exit;
}

// prepare a delete by id
$sql = 'DELETE FROM table WHERE id = ?';
$delrow = mysqli_prepare($conn,$sql);
if ( ! $delrow ) {
echo $delrow->error;
exit;
}

// Process all the delete candidates one by one
while ($row = $sel4del->fetch_object() ) {
// remove the file from disk
unlink($base_path . $row->image);
$del_count++;

// bind the current id to the prepared delete query
$delrow->bind_param('i', $row->id);

// execute the delete of this one row
$delrow->execute();
}
echo 'images removed = ' . $del_count
?>

关于php - 从已过期的行中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41988394/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com