所以我有以下代码片段,这样当我从数据库中删除一行时,我也会删除与之关联的文件,代码运行顺畅,我实际上能够从数据库中删除行但不知怎的,我'无法从服务器目录中删除文件,请注意,行“photo_filename”包含一个名称,如“photo.png”左右,同时从文件中调用_ DIR _返回一个路径,如这个“ ... \ Desktop \ project / procedures”,我甚至没有收到任何警告我试图回复一个字符串,如果取消链接成功和其他一些字符串如果不成功,但奇怪的是我没有得到任何输出,就好像循环甚至没有运行,有人能指出我正确的方向,我在这里缺少。谢谢
try {
$db->beginTransaction(); // Begin transaction
$query = "DELETE FROM properties "
. " WHERE property_id = :property_id"; // Delete requested property.
$stmt = $db->prepare($query);
$stmt->bindParam(":property_id", $property["property_id"], PDO::PARAM_INT);
$stmt->execute();
$query = "SELECT * FROM photos "
. " WHERE property_id = :property_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":property_id", $property["property_id"], PDO::PARAM_INT);
$stmt->execute();
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $photo) {
try {
unlink(__DIR__ . "/../img/" . $photo["image_filename"])
} catch (Exception $e) {
throw $e;
}
}
$query = "DELETE FROM photos "
. " WHERE property_id = :property_id";
$stmt = $db->prepare($query);
$stmt->bindParam(":property_id", $property["property_id"], PDO::PARAM_INT);
$stmt->execute();
$db->commit();
} catch (Exception $e) { // If there is a problem
$db->rollBack(); //If there was a problem undo the whole attempt to insert
$session->getFlashBag()->add("error", "Hubo un problema" . $e->getMessage()); // Display a message
redirect("/show.php?id=".$property_id); // And redirect
exit;
}
答案 0 :(得分:0)
我刚刚意识到我的愚蠢错误,所以"属性"表被链接到"照片"因此,当我开始交易并从表中删除属性时,与其关联的照片也会自动删除,所以当我选择并循环浏览照片表以获取文件时,没有任何内容,请将此处留在此处如果有人碰到类似的东西,谢谢你们回复。