取消链接 - 权限被拒绝。怎么解决?

时间:2017-12-25 08:08:40

标签: php file-upload unlink

我想使用unlink删除文件,但收到警告信息

Warning: unlink(uploads/487001/): Permission denied in

当我检查文件夹时,文件已成功删除。以下是我所做的代码,

Uploads.php

<?php
if (isset($_FILES['mc'])) {
    $file = $sno . "-" . date("Ymd") . "-" . $_FILES['mc']['name'];
    $file_loc = $_FILES['mc']['tmp_name'];
    $file_size = $_FILES['mc']['size'];
    $folder = "uploads/" . $sno . "/";
    $final_file = str_replace(' ', '-', strtolower($file));

    if ($file_size <= 0) {
        $error= "<span class=\"w3-text-red\">Select a file to upload</span>";
    }

    if (!is_dir($folder)) {
        mkdir($folder, 0777, true);
    }

    if (move_uploaded_file($file_location, $folder . $final_file)) {
        $upload = true; 
    }
}

Delete.php

<?php 
if(isset($_POST['delete'])){

    if(empty($_POST['checkbox'])){
        $error = "<span class=\"w3-container w3-text-red\">No data selected</span><p/>";
    } elseif(isset($_POST['checkbox'])){
        $checkbox = $_POST['checkbox'];
        $files = $_POST['file'];
        $path = 'uploads/' . $_SESSION['staff_no'] . '/';

        foreach ($files as $file) {
            if (file_exists($path.$file)) {
                chmod($path.$file, 0777);
                unlink($path.$file);
            } 
        }
    }

// Other validations & Delete row from database

这是文件结构,

Project Folder > Uploads > user_id > files

我可以知道错误是什么吗?

2 个答案:

答案 0 :(得分:1)

$ POST ['file']持有什么?如果它是一个数组,那么也检查空白/空值。如果 $ file 为空, file_exists()将返回true,但您的路径最终会以 uploads / 487001 / 结束,您将会得到那个警告。

答案 1 :(得分:0)

使用这种方式,它将从服务器以及数据库中删除文件。

if(isset($_POST['delete'])){

    if($file_path) {
         chmod($file_path, 0777);
         unlink($file_path);
       $query = "DELETE FROM medias WHERE mediaId = '".$_POST["delete"]."'";
       $statement= $db->delete($query);   
     }
}