php 如何获得使用 unlink() 的权限?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12148229/
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
How to get permission to use unlink()?
提问by user1628256
I make a site and it has this feature to upload a file and that file is uploaded to a server
我制作了一个网站,它具有上传文件的功能,并将该文件上传到服务器
Im just a newbie to php I download xampp and I run this site that i made in my local machine. My site is like this you upload a file then that file will be uploaded to a server, but when i tried unlink() because when i try to remove the filename to a database I also want to remove that pic on the server, but instead I got an error and it says "Permission denied".
我只是一个 php 新手,我下载了 xampp,然后运行了我在本地机器上创建的这个站点。我的网站是这样的,您上传一个文件,然后该文件将上传到服务器,但是当我尝试 unlink() 时,因为当我尝试将文件名删除到数据库时,我也想删除服务器上的那张图片,但是我收到一个错误,上面写着“权限被拒绝”。
question:
How can I got permission to use unlink();?
问题:
我怎样才能获得使用 unlink(); 的许可?
I only run this on my localmachine using xampp
我只使用 xampp 在我的本地机器上运行它
采纳答案by Ilia Rostovtsev
Permission denied error happens because you're trying to delete a file without having enough/right permissions for doing that.
发生权限被拒绝错误是因为您尝试删除文件而没有足够/正确的权限来执行此操作。
To do this you must be using superuser account or be the same user that have uploaded the file.
为此,您必须使用超级用户帐户或与上传文件的用户相同。
You can go to the directory from your command line and check the permissions that are set to the file.
您可以从命令行转到该目录并检查为该文件设置的权限。
The easiest solution is to loggin as administrator/root and delete the file.
最简单的解决方案是以管理员/root 身份登录并删除文件。
Here is another work around:
这是另一种解决方法:
// define if we under Windows
$tmp = dirname(__FILE__);
if (strpos($tmp, '/', 0)!==false) {
define('WINDOWS_SERVER', false);
} else {
define('WINDOWS_SERVER', true);
}
$deleteError = 0;
if (!WINDOWS_SERVER) {
if (!unlink($fileName)) {
$deleteError = 1;
}
} else {
$lines = array();
exec("DEL /F/Q \"$fileName\"", $lines, $deleteError);
}
if ($deleteError) {
echo 'file delete error';
}
And some more: PHP Manual, unlink(), Post 106952
I would recommend, always first to check PHP Manual (in case your question concerns PHP), just go to the page with function that you have problems with and just click search CTRL+Fin your browser and enter, for example, Windows, and as a result, in your case, you would find at least 7 related posts to that or very close to that what you were looking for.
还有一些:PHP 手册,unlink(),发布 106952
我建议,总是首先查看 PHP 手册(如果您的问题涉及 PHP),只需转到具有您遇到问题的功能的页面,然后CTRL+F在您的浏览器并输入,例如,,Windows因此,在您的情况下,您会找到至少 7 个相关的帖子,或者与您要查找的内容非常接近。
回答by Abid Hussain
Read this URL
阅读这个网址
I found this information in the comments of the function unlink()
我在函数 unlink() 的注释中找到了这个信息
Under Windows System and Apache, denied access to file is an usual error to unlink file. To delete file you must to change file's owern. An example:
在 Windows 系统和 Apache 下,拒绝访问文件是取消链接文件的常见错误。要删除文件,您必须更改文件的owern。一个例子:
<?php
chown($TempDirectory."/".$FileName,666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody"
unlink($TempDirectory."/".$FileName);
?>
So try something like this:
所以尝试这样的事情:
$Path = './doc/stuffs/sample.docx';
chown($Path, 666);
if ( unlink($Path) )
echo "success";
else
echo "fail";
EDIT 1
编辑 1
Try to use this in the path:
尝试在路径中使用它:
$Path = '.'.DIRECTORY_SEPARATOR.'doc'.DIRECTORY_SEPARATOR.'stuffs'.DIRECTORY_SEPARATOR.'sample.docx';

