php - 如何使用 Unlink() 函数

标签 php file unlink

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

代码:

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

它保持返回失败。 sample.docx 确实驻留在该特定路径上。请指教。

最佳答案

我找到了这个信息in the comments of the function 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:

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

所以尝试这样的事情:

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

chown($path, 666);

if (unlink($path)) {
    echo 'success';
} else {
    echo 'fail';
}

编辑 1

尝试在路径中使用它:

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

关于php - 如何使用 Unlink() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11463581/

相关文章:

javascript - 我在自动刷新网页内容时遇到问题

php - 从 while 循环输入数组,为每个输入传递 $_POST

android - 如何显示保存在android中的数据

PHP 无法在 fclose 后取消链接文件

php - 检查一个数组是否包含另一个数组的所有元素

php - $(this).next() 的问题

java - android:内部文件读取

file - 为什么.png文件的第一个字节是0x89?

PHP:即使文件存在且可写,unlink 也无法删除文件

php - 为什么 Unlink 不适用于此脚本?