簡體   English   中英

如何從PHP中的文件夾中刪除圖像

[英]How to delete an image from a folder in PHP

我正在嘗試從數據庫以及PHP5中的文件夾中刪除圖像,但無法刪除它。 這是我的代碼:

 <?php

   $obj=new Crud("localhost","root","","3g");
   class Crud{
     public $mysqli;
     public $data;

     public function __construct($host,$username,$password,$db_name) {
       $this->mysqli = new mysqli($host, $username, $password, $db_name);

       if (mysqli_connect_errno()) {
         echo "Error: Could not connect to database.";
         exit;
       } /*else{
         echo"Your Database successfully connected";    
       }*/
     }

     public function __destruct() {
       $this->mysqli->close();  
     }

     public function read() {
       $query="SELECT * FROM fashion";
       $result= $this->mysqli->query($query);
       $num_result=$result->num_rows;

       if ($num_result>0) {
         while($rows=$result->fetch_assoc()){
           $this->data[]=$rows;
           //print_r($rows);
         }

         return $this->data;
       }
     }

     public function delete($id){
       $query="DELETE FROM fashion WHERE id='$id'";
       $result= $this->mysqli->query($query) or die(mysqli_connect_errno() . "Data cannot inserted");

       if ($result) {
         header('location:fashion.php');    
       }
     }
   }
 ?>

通過使用上面的代碼,僅從數據庫中刪除了記錄,但是圖像保留在文件夾中。

要從文件夾中刪除圖像,您必須使用unlink

$file = "yourimage.jpg";
unlink($file);

如果圖像在任何其他目錄中,則必須指定完整目錄路徑,例如$file = "www/images/yourimage.jpg";

您正在尋找的解決方案是執行此操作的unlink功能- unlink鏈接並刪除文件。

您需要文件名和PHP當前工作目錄中文件的路徑才能使其正常工作。 Kamrans的答案說明了這一點。

所以:

    public function delete($id){
            $query = "SELECT filepath FROM fashion WHERE id = ? LIMIT 1";
            $fileLocale = $this->mysqli->prepare($query);
            $fileLocale->bind_param("i",$id);
            $fileLocale->execute();

        $result =  $fileLocale->get_result(); 
$fileLocale->close();
        while ($row = $result->fetch_array(MYSQLI_NUM))
        {
/***
* only 1 result is returned so easy to collect 
***/
            foreach ($row as $r)
            {
                $filepath = $r;
            }
        }
    unset($r,$row);       
    /*** 
    $filepath is now the string of the location of the file.
    ***/
        if (unlink($filepath)){
                   $query="DELETE FROM fashion WHERE id=? LIMIT 1";
                   $delete = $this->mysqli->prepare($query);
        $delete->bind_param("i",$id);
        $delete->execute();
        $affectedRows  = $delete->affected_rows;      
                   if ($affectedRows == 1) {
                     header('location:fashion.php');   
                die(); 
                   }
  • 使用header始終在其后放置一個die命令。
  • 使用unlink刪除文件,成功將返回TRUE。
  • filepath是數據庫中存儲文件位置的列。
  • 使用OOP原理連接和使用數據庫,使用准備好的語句而不是查詢,對您或您的代碼的FAR風險較小。
  • 例如,將LIMIT添加到您的SQL語句中,以便在出現問題時不會弄亂整個表。 安全措施。

在刪除數據庫引用之前,使用它來填充文件名,這是我假設存儲在數據庫中的文件名。 如果$ id是文件名,只需將其添加到系統命令中即可刪除該文件。 假設使用Linux。

 public function delete($id){

   exec('rm /path to file/' . $id);//delete the file via system command.

   $query="DELETE FROM fashion WHERE id='$id'";
   $result= $this->mysqli->query($query) or die(mysqli_connect_errno() . "Data cannot inserted");

   if ($result) {
     header('location:fashion.php'); 
     die();   
   }
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM