PHP取消链接权限被拒绝错误

时间:2016-04-12 07:47:13

标签: php file

我使用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';
             }

但我在Permission denied

时收到unlink错误

我检查了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?

1 个答案:

答案 0 :(得分:0)

  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