unlink PHP works when file is in root, not if file is in folder

孤街浪徒 提交于 2019-12-20 06:26:13

问题


So this one is pretty straight forward I want to delete a file on the server using PHP, I have:

$myfile = 'theone.png';
unlink($myfile);

This code deletes the file, howevere if the path to file is /images/theone.png, it doesn't work, I have tried images\theone.png with no luck.

If I try and connect with FTP I get the error message to say that cURL does not support the unlink function... Any help would be great.

Thanks Guys!


回答1:


What about:

$root = realpath($_SERVER['DOCUMENT_ROOT']);
$myfile = '$root/images/theone.png';
unlink($myfile);

Although to my knowledge, your attempted method should work, unless either I'm missing something, or you haven't included some code here that might be interfering with the unlink.




回答2:


__DIR__ - this magic constant contains current directory, in case that the file is in the same directory as your PHP script you can use:

unlink(__DIR__ . "/$myfile");

If the file is for example in one directory above your PHP script you can use:

unlink(__DIR__ . "/../$myfile");

If the directory has correct access rights it should work.



来源:https://stackoverflow.com/questions/23228805/unlink-php-works-when-file-is-in-root-not-if-file-is-in-folder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!