权限被拒绝 - php unlink

时间:2012-11-27 23:10:31

标签: php permissions unlink denied

我有两个文件: b.php和test.txt

<?php 
$b = "test.txt";
unlink($b);
?>

,错误是:警告:取消链接(test.txt)[function.unlink]:权限被拒绝

为什么呢? b.php和test.txt是777,同一组/登录

如果我在父目录上设置777,我可以执行取消链接,但我必须设置777并返回到755?

5 个答案:

答案 0 :(得分:25)

您(在运行b.php的过程中,无论是通过CLI还是网络服务器)都需要对文件所在目录的写访问权限。您正在更新目录内容,因此访问该文件是不够的。

请注意,如果您使用PHP chmod()函数将文件或文件夹的模式设置为777,则应使用0777确保将数字正确解释为八进制号。

答案 1 :(得分:18)

您首先要求使用fclose($handle);关闭文件,因为该文件正在使用中,因此不会删除该文件。首先关闭文件,然后尝试。

答案 2 :(得分:7)

除了其他朋友的所有答案之外,如果有人正在寻找一种方法来删除&#34;文件夹&#34;不是&#34;文件&#34; ,请注意文件夹必须由php rmdir() function删除,如果你想删除&#34;文件夹&#34;通过unlink(),您将遇到错误的警告消息,其中显示&#34;权限被拒绝&#34;

然而你可以制作文件夹&amp; mkdir()的文件,但删除文件夹(rmdir())的方式与删除文件的方式不同(unlink()

答案 3 :(得分:3)

// Path relative to where the php file is or absolute server path
chdir($FilePath); // Comment this out if you are on the same folder
chown($FileName,465); //Insert an Invalid UserId to set to Nobody Owner; for instance 465
$do = unlink($FileName);

if($do=="1"){ 
    echo "The file was deleted successfully."; 
} else { echo "There was an error trying to delete the file."; } 

试试这个。希望它有所帮助。

答案 4 :(得分:1)

文件权限没问题(0777)但我认为你在共享服务器上,所以正确删除你的文件使用; 1.创建文件的正确路径

// delete from folder
$filename = 'test.txt';
$ifile = '/newy/made/link/uploads/'. $filename; // this is the actual path to the file you want to delete.
unlink($_SERVER['DOCUMENT_ROOT'] .$ifile); // use server document root
// your file will be removed from the folder

如果收集了实际的文件路径,那么小代码就可以完成魔术并从任何文件夹中删除任何所需的文件。