我仍然在我的食谱书中工作,在这一点上,我试图删除食谱的图像,如果这被删除。
我的数据库有一个食谱表,列"id","name"answers"attachment_id"。还有一个名为attachments的表,列为"id"answers"path_to_attachment"。(我明白在两个表中有这个无用的点,我只需要练习移动数据库并与它们交互)。
经过昨天的大量折腾后,我为这个attachment_id添加了一个触发器,因此,当一个食谱被删除时,它的附件和路径的行也被删除。
问题?图像提醒在"images/"文件夹中。当配方及其附件从数据库中删除时,我现在正试图删除图像…
图像存储在这个文件夹中,在recipe.php:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_STRING);
$attachment_id = filter_input(INPUT_POST, 'attachment_id', FILTER_SANITIZE_NUMBER_INT);
$folder="images/";
$file = $_FILES['photo']['tmp_name'];
$file_to_upload = $folder . basename($_FILES['photo']['name']);
if(move_uploaded_file($file, $file_to_upload)) {
echo "File is valid, and was successfully uploaded.'n";
if($attachment_id = add_image($file_to_upload)) {
if(add_recipe($name, $attachment_id)) {
header('Location: index.php');
exit;
} else {
$error_message = "Could not add recipe";
}
} else {
$error_message = "Could not add image";
}
} else {
echo 'Upload failure';
}
}
然后,在index.php上,我获得附件和食谱,每个食谱旁边都有一个按钮用于删除:
$recipes = get_recipes()
$attachments = get_attachments();
$attachment_path = find_path_by_id($recipe['attachment_id']);
<a class="toLink" href="delete_recipe.php?id=' . $recipe['id'] . '" title="delete recipe" onclick="return confirm('Are you sure you want to delete this recipe?');">Delete recipe</a>
在delete_recipe.php :
$recipeId = filter_input(INPUT_GET, 'id', FILTER_SANITIZE_NUMBER_INT);
$attachment_path ?????
if(delete_recipe($recipeId) == true) {
delete_attachment($attachment_path);
echo $attachment_path; die();
header('Location: index.php');
exit;
} else {
$error_message = "Could not delete recipe";
}
在fuctions.php :
function get_recipes() {
include'db_connection.php';
try {
return $conn->query("SELECT * FROM recipes");
} catch (PDOException $e) {
echo 'Error:' . $e->getMessage() . "<br />";
return array();
}
return true;
}
function get_attachments() {
include'db_connection.php';
try {
return $conn->query("SELECT * FROM attachments");
} catch (PDOException $e) {
echo 'Error:' . $e->getMessage() . "<br />";
return array();
}
return true;
}
function find_path_by_id($attachment_id = ':attachment_id') {
include 'db_connection.php';
$sql = 'SELECT * FROM attachments WHERE id=:attachment_id LIMIT 1';
try {
$results = $conn->prepare($sql);
$results->bindParam(':attachment_id', $attachment_id, PDO::PARAM_INT);
$results->execute();
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage() . '<br />';
return array();
}
return $results->fetch(PDO::FETCH_ASSOC);
}
function add_image($attachment_path= ':attachment_path') {
include 'db_connection.php';
try {
$sql = "INSERT INTO attachments(attachment_path) VALUES (:attachment_path)";
$results = $conn->prepare($sql);
$results->bindParam(':attachment_path', $attachment_path, PDO::PARAM_STR, 100);
$results->execute();
$id = $conn->lastInsertId();
$conn = null;
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage() . '<br />';
return false;
}
return $id;
}
function display_image($attachment_id = ':attachment_id') {
include 'db_connection.php';
$sql = 'SELECT * FROM attachments WHERE id=:attachment_id LIMIT 1';
try {
$results = $conn->prepare($sql);
$results->bindParam(':attachment_id', $attachment_id, PDO::PARAM_INT);
$results->execute();
} catch(PDOException $e) {
echo 'Error: ' . $e->getMessage() . '<br />';
return array();
}
return $results->fetch(PDO::FETCH_ASSOC);
}
问题是,我无法获得attachment_path一旦我点击删除配方。我试图添加,但我不想把它发送到url上,我无法得到路径一旦它正在处理"删除配方"。
我的想法是,一旦食谱被删除,我创建一个函数来查找图像目录内的文件的名称,然后,我删除它,但是,正如我所说的,我不知道如何将图像名称传递给delete_recipe.php文件。
我想这一定是一个更合乎逻辑的方式做这件事....但我不知道怎么…有什么建议吗?
谢谢!
使用取消链接功能从文件夹unlink($attachment_path)
中删除图像,附件路径应为例如images/abc.jpg
// image path return from delete_recipe() function
$attachment_path = delete_recipe($recipeId);
if(isset($attachment_path)) {
unlink($attachment_path);
header('Location: index.php');
} else {
$error_message = "Could not delete recipe";
}