【问题标题】:PHP unlink permission denied errorPHP取消链接权限被拒绝错误
【发布时间】:2016-04-12 07:47:12
【问题描述】:

我已经使用php GD library 调整图像大小并首先将它们存储在本地。

调整大小后,我将这些图片上传到server

我现在想从本地目录中删除图片。

这是我的代码..

          $image_name = $_FILES["imagePath"]["name"];
            $newimg = 'app/templates/'.TEMPLATE.'/memberimages/'.$image_name;

           $filename = realpath($newimg);

            if (is_file($filename)) {

                chmod($filename, 0777);

                if (unlink($filename)) {
                   echo 'File deleted';
                } else {
                   echo 'Cannot remove that file';
                }

             } else {
               echo 'File does not exist';
             }

但我在unlink 收到Permission denied 错误

我检查了memberimages 文件夹和php is_writable(),它也返回true。

我也试过exec()。没有错误但无法删除图片。

需要帮助。

If I am able to save images in that folder then delete should work as well. Is it really permissions related issue?

【问题讨论】:

  • 文件夹需要有可执行权限,显然没有。
  • 检查上传的文件是否属于运行 PHP 脚本的同一系统用户(通常为“www”、“httpd”或“apache”)。
  • 如何查看用户?
  • @AD7six 我检查了is_writable 的文件夹。返回真。还添加了0777 到文件夹以及仍然无法正常工作

标签: php file


【解决方案1】:
  1. 确保您尝试传递给unlink()/delete 的文件未被打开。如果文件是.exe 或等,请同时检查 Windows 任务管理器 -> 进程,然后将其杀死(在这种情况下,我认为文件是图像)。
  2. 更改文件的权限状态。也许您无权删除文件(执行权限)。

在你确定文件没有被打开1之后,让我们试试这个代码:

// Check existence of file
if (file_exists($cekFile1)) {
    // make sure you have permission to delete file
    if(chmod($fileDir, 0777)){
        if(!unlink($cekFile1)){
            echo "unlink is fail !";
        }
    }else{
        echo "chmod is fail";
    }
    // owner have read, write, execute rights.
    // owner's user group only have read rights.
    // everybody else only have read rights.
    chmod($fileDir, 0744);

}

有关chmod()的更多信息,请查看此参考:

php.net.

php-cmod-function.

【讨论】:

    猜你喜欢
    • 2012-11-15
    • 2017-12-23
    • 2011-08-07
    • 1970-01-01
    • 2019-06-26
    • 1970-01-01
    • 2020-06-21
    • 2020-01-08
    • 2014-12-24
    相关资源
    最近更新 更多