加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP:即使文件存在且可写,unlink也无法删除文件

发布时间:2020-12-13 17:25:34 所属栏目:PHP教程 来源:网络整理
导读:我一直试图弄清楚为什么取消链接不起作用.我已经尝试过stackoverflow以前的问题和答案,但没有运气.需要删除的确切文件名是“upload / test.png”. 首先,我检查文件是否存在. $filename = 'upload/test.png';if(file_exists($filename)){// file_exists retur
我一直试图弄清楚为什么取消链接不起作用.我已经尝试过stackoverflow以前的问题和答案,但没有运气.需要删除的确切文件名是“upload / test.png”.
首先,我检查文件是否存在.

$filename = 'upload/test.png';
if(file_exists($filename)){
// file_exists returns true
    if(is_writable($filename)){
        // is_writable also returns true
        if(unlink($filename)){
            echo 'file deleted';
        }
        else{
            echo 'cant delete file';
            print_r(error_get_last());
            // this gives me
            // unlink() function.unlink: No such file or directory
        }
    }
}

解决方法

改为给出完整的路径,比如

$filename = dirname(__FILE__) . '/upload/test.png';

试试这个,

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';
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读