dpt8910 2016-11-15 10:36
浏览 52
已采纳

通过mysql触发器从db表中删除路径后从文件夹中删除图像

I am still working in my recipe book, at this point, I am trying to remove the image for the recipe if this gets removed.

My database has a table for recipes, with columns "id", "name" and "attachment_id". Also a table called attachments with "id" and "path_to_attachment" columns. (I understand the useless point of having this in two tables, I just need to practise moving around databases and interacting with them).

After lots of messing around yesterday, I added a trigger for this attachment_id, so, when a recipe gets removed, the row for its attachment and path is also removed.

The problem? The image reminds in the "images/" folder. I am now trying to remove the image when the recipe and its attachments are removed from the database...

The image is stored on this folder with this, on 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.
";


   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';
   }   
}

Then, on index.php, I get the attachments and recipes, and I have a button next to each recipe for it to be deleted:

$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>

On 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); 
            }

The problem is, I can't get the attachment_path once I clicked delete recipe. I have tried to add in but I don't want to send it on the url, and I can't get the path once it is processing the "delete recipe".

My thoughts were,once the recipe gets deleted, I create a function to find the name of the file inside the images directory and then, I delete it, but, as I said, I don't know how can I pass the image name to the delete_recipe.php file.

I guess it must be a more logical way of doing this.... but I don't know how... any suggestions?

Thank you!

  • 写回答

1条回答 默认 最新

  • dq804806 2016-11-15 10:40
    关注

    Use unlink function for delete image from folder unlink($attachment_path), attachment path should be for example 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";
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作