如何使用Unlink()函数

JLe*_*ner 11 php file unlink

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

码:

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

它会让回报失败.sample.docx确实驻留在该特定路径上.好心提醒.

Mar*_*ato 10

在函数unlink()的注释中找到了这些信息

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

chown($tempDirectory . '/' . $fileName, 666); //Insert an Invalid UserId to set to Nobody Owern; 666 is my standard for "Nobody" 
unlink($tempDirectory . '/' . $fileName); 
Run Code Online (Sandbox Code Playgroud)

所以尝试这样的事情:

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

chown($path, 666);

if (unlink($path)) {
    echo 'success';
} else {
    echo 'fail';
}
Run Code Online (Sandbox Code Playgroud)

编辑1

尝试在路径中使用它:

$path = '.'
         . DIRECTORY_SEPARATOR . 'doc'
         . DIRECTORY_SEPARATOR . 'stuffs'
         . DIRECTORY_SEPARATOR . 'sample.docx';
Run Code Online (Sandbox Code Playgroud)


Tra*_*vis 8

试试这个:

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

如果你得到的文件不存在,你的路径就错了.如果不是,则可能是权限问题.