簡體   English   中英

從文件夾刪除文件

[英]Deleting file from folder

我不太確定問題出在哪里。 但是代碼不會取消鏈接文件:(

 <?php include_once("sessions.php");
require_once("connect.php");
if(isset($_POST['delete'])){

$album_id = $_SESSION['album_id'];

$checkbox = $_POST['photo_checkbox'];
$count = count($checkbox);

for($i = 0; $i < $count; $i++) {
    $id = (int) $checkbox[$i]; // Parse your value to integer

    if ($id > 0) { // and check if it's bigger then 0

        $query = "SELECT * FROM media WHERE id = $id";
        $result = mysqli_query($connection, $query);
            while($row = mysqli_fetch_array($result)){

            $file = $row['path'];

                if(!unlink($file)){
                    $_SESSION["edit_message"] = "<br>Something went wrong while deleting shit ... please try your editing again." .$file;
                    header ("Location: ../fotos.php?album=".$album_id."");
                    exit;
                }

            }
        $query = "DELETE FROM media WHERE id = $id";
        $result = mysqli_query($connection, $query);
    }
}   

    if($result){
        $_SESSION["edit_message"] = "<br>Successfully deleted !";
        header ("Location: ../fotos.php?album=".$album_id."");
        exit;}
}

?>

如果我去掉unlink循環部分,然后直接從數據庫中刪除它就可以了。 我想念什么? 可能是權限阻礙了代碼的執行?

編輯:現在將文件的權限更改為0777。 因此它應該確實起作用。但是似乎仍然沒有。 :/我現在沒有想法。 也許循環工作不正常?

感謝您的幫助

干杯

克里斯

$file2 = chmod($file, 0777);

if(!unlink($file2)){

$ file2正在獲取chmod的返回值,這是一個布爾值。 然后,您嘗試取消鏈接true / false值。 也許您打算取消鏈接($ file)?

編輯以反映您的更改:

如果$ file不是完全限定的路徑名​​,則$ file將相對於腳本運行所在位置的當前工作目錄。 確保$ file是完整路徑名。

對文件的寫權限還不夠,您需要對目錄本身具有寫權限才能刪除其中的文件。

您應該首先檢查文件是否存在,然后再檢查對目錄(不是文件)的正確權限。

if(file_exists($file) && is_writeable(dirname($file))){
unlink($file);
}else{
//invalid path or permission problems
}

暫無
暫無

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

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