我正在尝试使用该函数从目录中删除图像unlink()。
if (file_exists($oldPicture)) {
unlink($oldPicture);
echo 'Deleted old image';
}
else {
echo 'Image file does not exist';
}
Run Code Online (Sandbox Code Playgroud)
$oldPicture的值为../images/50/56/picture.jpg。
以下代码块运行时没有任何错误,但是当我检查目录时图像仍然存在。
我在这里做错了什么吗?
谢谢
\n\n\n“@Fred-ii-是的,每个人都是对的,我的权限被拒绝了...知道如何解决这个问题吗?PS,错误报告很棒,这真的会帮助我,谢谢!\xe2\x80\x93 BigRabbit ”
\n
在取消链接之前对文件使用chmod :
\n\nif (file_exists($oldPicture)) {\n\n// last resort setting\n// chmod($oldPicture, 0777);\nchmod($oldPicture, 0644);\n unlink($oldPicture);\n echo \'Deleted old image\';\n} \nelse {\n echo \'Image file does not exist\';\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n\n最后的手段设置将使用0777注释代码中的内容。
\n\n\n“我如何使用 var_dump() 来检查文件夹/文件权限?”
\n
IE:var_dump($oldPicture);
if (file_exists($oldPicture)) {\n\nvar_dump($oldPicture);\n\n// last resort setting\n// chmod($oldPicture, 0777);\nchmod($oldPicture, 0644);\n unlink($oldPicture);\n echo \'Deleted old image\';\n} \nelse {\n echo \'Image file does not exist\';\n}\nRun Code Online (Sandbox Code Playgroud)\n\n将错误报告添加到文件顶部,这将有助于查找错误。
\n\n<?php \nerror_reporting(E_ALL);\nini_set(\'display_errors\', 1);\n\n// rest of your code\nRun Code Online (Sandbox Code Playgroud)\n\n旁注:错误报告只能在暂存阶段完成,而不能在生产阶段完成。
\n