php Unlink 报错:Permission Denied

坑1:php Unlink出现Permission Denied的原因之一 :Parent Directory未授予写权限

该篇文章指出了该现象的一种原因:

You (as in the process that runs b.php, either you through CLI 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 to 777 you should use 0777 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”

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值