繁体   English   中英

unlink()不适用于绝对路径

[英]unlink() doesn't work with absolute path

我正在研究使用unlink()删除图片的方法。 但是我找不到一种使用绝对路径使其工作的方法。

这是我的代码:

$img = $_SERVER['DOCUMENT_ROOT'].'/i/koala.png';
unlink($img);

错误:

Warning: unlink(/var/www/html/i/koala.png): No such file or directory in /var/www/html/king/test.php on line 15

有什么帮助吗?

将各种字符串拼凑在一起以构成路径时,应使用realpath进行验证,并转换/../等任何相对片段。

http://php.net/manual/zh/function.realpath.php

$path = $_SERVER['DOCUMENT_ROOT'].'/i/koala.png';
$img = realpath($path);

$ img将为false或有效路径的字符串!

现在检查

$filename = '/var/www/html/i/koala.png';

if(file_exists($filename))
{
  chmod($filename, 777);
  unlink($filename);
  echo "file has deleted";
}
else
{
 echo "file not exists";
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM