php 如何使用 Unlink() 函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11463581/
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 00:34:34  来源:igfitidea点击:

How to use Unlink() function

phpfileunlink

提问by JLearner

I'm trying to use PHP unlink()function to delete away the specific document in the folder. That particular folder has already been assigned to full rights to the IIS user.

我正在尝试使用PHP unlink()函数删除文件夹中的特定文档。该特定文件夹已分配给 IIS 用户的完全权限。

Code:

代码:

$Path = './doc/stuffs/sample.docx';
if (unlink($Path)) {    
    echo "success";
} else {
    echo "fail";    
}

It keep return fail. The sample.docx does reside on that particular path. Kindly advise.

它保持返回失败。sample.docx 确实驻留在该特定路径上。好心提醒。

采纳答案by Marcio Mazzucato

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 the file's owner. An example:

在 Windows 系统和 Apache 下,拒绝访问文件是取消链接文件的常见错误。要删除文件,您必须更改文件的所有者。一个例子:

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';

回答by Travis

Try this:

尝试这个:

$Path = './doc/stuffs/sample.docx';
if (file_exists($Path)){
    if (unlink($Path)) {   
        echo "success";
    } else {
        echo "fail";    
    }   
} else {
    echo "file does not exist";
}

If you get file does not exist, you have the wrong path. If not, it may be a permissions issue.

如果你得到的文件不存在,你的路径是错误的。如果没有,则可能是权限问题。

回答by leet

This should work once you are done with the permission issue. Also try

一旦您完成权限问题,这应该可以工作。也试试

ini_set('display_errors', 'On');  

That will tell you whats wrong

那会告诉你出了什么问题

回答by antelove

define("BASE_URL", DIRECTORY_SEPARATOR . "book" . DIRECTORY_SEPARATOR);
define("ROOT_PATH", $_SERVER['DOCUMENT_ROOT'] . BASE_URL);

$path = "doc/stuffs/sample.docx";

if (unlink(ROOT_PATH . $Path)) {   
  echo "success";
} else {
  echo "fail";    
}

// http://localhost/book/doc/stuffs/sample.docx
// C:/xampp/htdocs\book\doc/stuffs/sample.docx

回答by James Woodruff

You need the full file path to the file of interest. For example: C:\doc\stuff\sample.docx. Try using __DIR__or __FILE__to get your relative file position so you can navigate to the file of interest.

您需要感兴趣文件的完整文件路径。例如:C:\doc\stuff\sample.docx。尝试使用__DIR____FILE__获取您的相对文件位置,以便您可以导航到感兴趣的文件。