Delete第一行显示图像的名称,第二行显示位于名为 images 的目录中的图像。 我可以使用以下代码删除该行 prepare("-6ren">
gpt4 book ai didi

php - 如何从文件夹和数据库中删除图像

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

我使用下面的代码在表中显示来自 mysql 数据库的数据

<td><?php echo $row['name']; ?></td>
<td><?php echo $row['image']; ?></td>
<a title="Delete" href="deletestudent.php?id=<?php echo $row['id']; ?>"><button class="btn btn-danger btn-mini"> Delete</button></a></td>
第一行显示图像的名称,第二行显示位于名为 images 的目录中的图像。

我可以使用以下代码删除该行

<?php
include('../connect.php');
$id=$_GET['id'];
$result = $db->prepare("DELETE FROM student WHERE id= :memid");
$result->bindParam(':memid', $id);
$result->execute();

header ("location: students.php");
?>
但我怎样才能从图像文件夹中删除图像。

提前致谢。

最佳答案

在从数据库中删除图像之前,您应该先删除该图像,如果成功,则从数据库中删除该图像。

您可以使用php的unlink功能删除图像。在此之前确保文件存在:

<?php
include('../connect.php');
$id=$_GET['id'];

// you must first retrieve data from database to get the image path that
//you have been saved in db

$stmt = $dbh->prepare("SELECT * FROM mytable WHERE id= :memid LIMIT 1");
$stmt->bindParam(':memid', $id);
$stmt->execute();

$record = $stmt->fetch();

//get image path
$imageUrl = $_DIR_.'/images/uploads/profile/'.$record['Image_name'];

//check if image exists
if(file_exists($imageUrl)){

//delete the image
unlink($imageUrl);

//after deleting image you can delete the record
$result = $db->prepare("DELETE FROM student WHERE id= :memid");
$result->bindParam(':memid', $id);
$result->execute();
}
header ("location: students.php");
?>

关于php - 如何从文件夹和数据库中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55091471/

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