php - 通过mysql触发器从数据库表中删除路径后从文件夹中删除图像

标签 php mysql image

我仍在处理我的食谱书,此时,我正在尝试删除食谱的图像(如果该图像被删除)。

我的数据库有一个食谱表,其中包含“id”、“name”和“attachment_id”列。还有一个名为“附件”的表,其中包含“id”和“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。我尝试添加,但我不想在网址上发送它,并且一旦处理“删除食谱”,我就无法获取路径。

我的想法是,一旦食谱被删除,我创建一个函数来查找图像目录中的文件名,然后将其删除,但是,正如我所说,我不知道如何通过将图像名称添加到delete_recipe.php 文件中。

我想这一定是一种更合乎逻辑的方式......但我不知道如何......有什么建议吗?

谢谢!

最佳答案

使用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";
}

关于php - 通过mysql触发器从数据库表中删除路径后从文件夹中删除图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40607638/

相关文章:

php - 什么是 netbeans 中的重要文件文件夹项目

image - 为什么在 jekyll 网站上添加图像的 Markdown 语法不起作用?

php - 无法使用 TinyMCE 编辑器通过 Ajax 填充文本区域

c# - 在 DLL 上使用 WPF 动态创建图像(而不是 GDI+)

javascript - 来自 Google 身份验证提供程序的 Firebase photoURL 返回颜色反转的 jpg

php数字数组选择大于数字且小于另一个的值并将其保存到新数组

php - 连接 2 个 mysql 查询

php - 拉维尔 5 : How to store extra attributes to user login session

mysql - "MySQL Enterprise Edition Subscription (1-4 socket server)"中的套接字是什么

mysql - 如何仅使用一个查询来执行此 php 循环和多个 mysql 查询?