坑1:php Unlink出现Permission Denied的原因之一 :Parent Directory未授予写权限
该篇文章指出了该现象的一种原因:
You (as in the process that runs
b.php
, either you throughCLI
or a webserver) need write access to the directory in which the files are located. You are updating the directory content, so access to the file is not enough.Note that if you use the PHP
chmod()
function to set the mode of a file or folder to777
you should use0777
to make sure the number is correctly interpreted as an octal number.
即用户不具备需要被删除文件所在的文件夹的写权限。下面将通过实验证明这一点:
1. 以root用户的身份在网站根目录下创建文件夹"tardir",赋予权限705,进入文件夹,创建文件1.txt,赋予权限777
2. 在网站根目录下创建如下的php文件,预期结果如注释所示:
<?php
# www, a user different from root
echo `whoami`.'<br/>';
# read ./tardir/1.txt , successful
echo `cat ./tardir/1.txt`.'<br/>';
echo unlink('./tardir/1.txt');
# Failed, since write access to the parent direcotry is forbidden
echo `touch ./c.txt`;
# Failed
?>
因此,当发现针对文件的操作返回异常结果时,不妨检查一下其所在文件夹的权限。
坑2:rmdir与unlink
<?php
symlink('tardir','tplnk');
unlink('tplnk');
?>
PHP版本为5.3
在文件夹"tardir"存在的前提下,windows环境下执行该脚本文件会报错“Permission Denied”,而Linux环境下则不会。
在windows环境下的解决方法是,把unlink替换为rmdir指令。
额外地,如果在linux环境中将unlink指令换成rmdir指令,则会报错“rmdir(tplnk): Not a directory”