我有两个文件:b.php和test.txt
<?php
$b = "test.txt";
unlink($b);
?>
错误提示:警告:unlink(test.txt) [function.unlink]: Permission denied
为什么?b.php 和 test.txt 都是 777,且属于同一组/登录用户。
如果我将父级目录设置为 777,我可以执行 unlink,但我必须将权限设为 777,然后再设为 755?
我有两个文件:b.php和test.txt
<?php
$b = "test.txt";
unlink($b);
?>
错误提示:警告:unlink(test.txt) [function.unlink]: Permission denied
为什么?b.php 和 test.txt 都是 777,且属于同一组/登录用户。
如果我将父级目录设置为 777,我可以执行 unlink,但我必须将权限设为 777,然后再设为 755?
你(作为运行b.php的进程,无论是通过CLI还是Web服务器)需要对包含文件的目录拥有写入访问权限。你正在更新目录内容,因此仅对文件进行访问是不够的。
请注意,如果你使用PHP的chmod()
函数将文件或文件夹的模式设置为777
,则应使用0777
以确保数字被正确解释为八进制数。
chmod("/somedir/somefile", 777);
https://www.php.net/manual/en/function.chmod.php 还要注意:文件夹本身必须对所有人可执行。 - Avatar您需要首先使用fclose($handle);
关闭文件,因为该文件正在使用中,所以无法删除。因此,请先关闭文件,然后再尝试删除。
unlink()
删除“文件夹”,将会遇到错误警告信息“权限被拒绝”。mkdir()
创建文件夹和文件,但是删除文件夹(rmdir()
)的方式与删除文件(unlink()
)的方式不同。例如,如果您想使用在许多编程语言中,任何与权限相关的错误都可能不直接表示实际的权限问题。
node fs模块
readSync
读取不存在的文件,则会遇到错误的EPERM
错误。// 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."; }
试一下这个,希望能有所帮助。
// 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
在Windows和PHP版本7.3.0之前,请检查您的文件在unlink之前是否已关闭,
如https://www.php.net/manual/en/function.unlink.php所述:
在Windows上,现在可以unlink()正在使用句柄的文件,而以前会失败。但是,在所有对其的句柄关闭之前,仍无法重新创建未链接的文件。
例如:
$fullFilePath = 'C:\Users\MyUserName\www\myApp\public\test.txt';
$handle = fopen($fullFilePath , 'w+');
fopen($filePath, 'w+');
fputs($handle, 'Some text in the file');
fclose($handle);
unlink(realpath($insertedLinesFilePath));