从已过期的行中删除图像
delete image from row that has been expired
嘿,我有一个 php 代码可以检查哪些行日期已过期并删除它们,其中一列是通过 ftp 上传到服务器的图像名称,当我删除所有过期的记录我还想删除附加到它们的图像。
我在 php 上阅读了有关取消链接命令的信息,但我不知道如何将它一次应用于所有图像。
我有这段代码,例如我只是漫无目的,因为我真的不知道该怎么做..
$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";
}
?>
谁能告诉我如何删除所有附加到已删除行的图像?
如果您想从数据库中的 table 中删除行,同时从保存它们的服务器上的文件夹中删除图像,那么您将必须处理每个删除候选一次,这样你就可以从行中获取文件名来完成文件删除的同时删除行。
// 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 代码可以检查哪些行日期已过期并删除它们,其中一列是通过 ftp 上传到服务器的图像名称,当我删除所有过期的记录我还想删除附加到它们的图像。 我在 php 上阅读了有关取消链接命令的信息,但我不知道如何将它一次应用于所有图像。
我有这段代码,例如我只是漫无目的,因为我真的不知道该怎么做..
$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";
}
?>
谁能告诉我如何删除所有附加到已删除行的图像?
如果您想从数据库中的 table 中删除行,同时从保存它们的服务器上的文件夹中删除图像,那么您将必须处理每个删除候选一次,这样你就可以从行中获取文件名来完成文件删除的同时删除行。
// 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
?>