我仍然在我的食谱中工作,此时,我正在尝试删除配方的图像,如果这被删除。
我的数据库有一个食谱表,列有" id"," name"和" attachment_id"。还有一个名为附件的表,其中包含" id"和" path_to_attachment"列。 (我理解在两个表中使用它的无用点,我只需要练习在数据库中移动并与它们进行交互)。
在昨天发生了很多乱码之后,我为这个attachment_id添加了一个触发器,因此,当一个配方被删除时,它的附件和路径的行也会被删除。
问题?图像提醒"图像/"夹。我正在尝试从数据库中删除配方及其附件时删除图像...
图像存储在此文件夹中,在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";
}
on 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。我试图添加,但我不想将其发送到网址上,一旦处理完&#34;删除配方&#34;我就无法获得路径。
我的想法是,一旦配方被删除,我创建了一个函数来查找images目录中文件的名称,然后,我删除它,但是,正如我所说,我不知道怎么能我将图像名称传递给delete_recipe.php文件。
我想这一定是一种更合乎逻辑的做法....但我不知道......有什么建议吗?
谢谢!
答案 0 :(得分:1)
使用unlink功能删除文件夹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";
}