簡體   English   中英

從php的存儲文件夾中刪除圖像

[英]deleting image from the storage folder in php

我也想在從數據庫中刪除時從存儲文件夾中刪除圖像。

圖像文件已從數據庫中刪除,但無法從服務器存儲文件夾中刪除。

圖片已存儲在http://url.com/foldername/files/newsletter中

下面是我使用的代碼。

<?php

$id = intval($_REQUEST['id']);

include 'db/connection.php';



$sql1 = mysql_query("select * from tablename where id=$id");
$results=mysql_fetch_array($sql1);

if($results["file"]!="") {  
    $image=$results["file"];
    unlink("../files/newsletter/".$image);
    }



$sql = "delete from tablename where id=$id";

$result = @mysql_query($sql);


if ($result){
    echo json_encode(array('success'=>true));
} else {
    echo json_encode(array('msg'=>'Some errors occured.'));
}

?>

請幫忙解決...謝謝

嘗試將帶有您代碼的文檔根目錄添加到下面的代碼中。

$imageWithPath = $_SERVER['DOCUMENT_ROOT']."/files/newsletter/".$image;
@unlink($imageWithPath);

您可以打印出$ results [“ file”]值是什么並檢查一下。

您正在使用mysql_fetch_array,它應該在while循環內

$sql1 = mysql_query("select * from tablename where id=$id");
while($row = mysql_fetch_array($sql1)){
    $image = $row["file"];
    //make sure below path is correct.
    $image_path = $_SERVER['DOCUMENT_ROOT'].'/foldername/files/newsletter/'.$image;
    echo $image_path;//you can compare if the path is correct or not
    //Also check if file exists here
    if(file_exists(image_path)){
       unlink($image_path);
    }
    else{
      echo 'file doesnot exist';
    }
}

另外,您將需要具有刪除文件的權限。 確保運行腳本文件的apache或用戶具有正確的權限或文件所有權。

暫無
暫無

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

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