权限被拒绝 - php unlink

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13594898/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 05:44:13  来源:igfitidea点击:

permission denied - php unlink

phppermissionsunlinkdenied

提问by eugui

I have two files: b.php and test.txt

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

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

and the error is: Warning: unlink(test.txt) [function.unlink]: Permission denied

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

why? b.php and test.txt is 777 and the same group/login

为什么?b.php 和 test.txt 是 777 并且同组/登录

if I set 777 on the parent directory I can execute unlink but i have to set 777 and back to 755?

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

回答by Arjan

You (as in the process that runs b.php, either you through CLIor 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.

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

Note that if you use the PHP chmod()function to set the mode of a file or folder to 777you should use 0777to make sure the number is correctly interpreted as an octal number.

请注意,如果您使用 PHPchmod()函数来设置文件或文件夹的模式,777您应该使用它0777来确保数字被正确解释为八进制数。

回答by Ashwin Pathak

You'll first require to close the file using fclose($handle);it's not deleting because the file is in use. So first close the file and then try.

您首先需要关闭该文件fclose($handle);,因为该文件正在使用中,所以它不会被删除。所以先关闭文件再试试。

回答by ashkan nasirzadeh

in addition to all the answers that other friends have , if somebody who is looking this post is looking for a way to delete a "Folder" not a "file", should take care that Folders must delete by php rmdir() functionand if u want to delete a "Folder" by unlink(), u will encounter with a wrong Warning message that says "permission denied"

除了其他朋友的所有答案之外,如果正在查看这篇文章的人正在寻找一种删除“文件夹”而不是“文件”的方法,应该注意文件夹必须通过php rmdir() 函数删除,如果你想删除一个“文件夹” unlink(),你会遇到一个错误的警告消息,上面写着“权限被拒绝”

however u can make folders & files by mkdir()but the way u delete folders (rmdir()) is different from the way you delete files(unlink())

但是您可以通过mkdir()以下方式创建文件夹和文件,但是rmdir()您删除文件夹的方式 ( ) 与删除文件的方式不同 ( unlink())

eventually as a fact:

最终成为事实:

in many programming languages, any permission related error may not directly means an actual permission issue

在许多编程语言中,任何与权限相关的错误可能并不直接意味着实际的权限问题

for example, if you want to readSynca file that doesn't exist with node fs moduleyou will encounter a wrong EPERMerror

例如,如果你想要readSync一个不存在的文件,node fs module你就会遇到错误的EPERM错误

回答by Ardi

// 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."; } 

Try this. Hope it helps.

尝试这个。希望能帮助到你。

回答by dean

The file permission is okay (0777) but i think your on the shared server, so to delete your file correctly use; 1. create a correct path to your file

文件权限没问题(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

That small code will do the magic and remove any selected file you want from any folder provided the actual file path is collect.

如果收集了实际文件路径,那么该小代码将发挥作用并从任何文件夹中删除您想要的任何选定文件。