[英]unlink() doesn't work with absolute path
I'm working on a method which delete pictures using unlink()
. 我正在研究使用unlink()
删除图片的方法。 However I can't find a way to make it work using absolute path. 但是我找不到一种使用绝对路径使其工作的方法。
Here is my code : 这是我的代码:
$img = $_SERVER['DOCUMENT_ROOT'].'/i/koala.png';
unlink($img);
Error : 错误:
Warning: unlink(/var/www/html/i/koala.png): No such file or directory in /var/www/html/king/test.php on line 15
Any help? 有什么帮助吗?
When you cobble together various strings to make a path, you should use realpath
to verify it and convert any relative fragments like /../
. 将各种字符串拼凑在一起以构成路径时,应使用realpath
进行验证,并转换/../
等任何相对片段。
http://php.net/manual/en/function.realpath.php http://php.net/manual/zh/function.realpath.php
$path = $_SERVER['DOCUMENT_ROOT'].'/i/koala.png';
$img = realpath($path);
$img will either be false, or a string of a valid path! $ img将为false或有效路径的字符串!
check it now 现在检查
$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.