我想要一个列表或代码块,其中包含防止和取消链接函数引发错误所需的所有必要检查。越详细越好,因为我希望这个问题能解决很多人的问题。
错误的原因
- 文件不存在
- 文件不可写入
- 包含目录不可写@mrc anton dahman答案
- 文件打开/正在使用
这就是我目前拥有的
if(is_actually_file($file)){
if(is_writable($file) && is_writable(dirname($file))) {
unlink($file);
} else {
// insufficient file permissions
}
} else {
// file doesn't exist
}
function is_actually_file($file){
clearstatcache(true, $file);
return is_file($file);
}
失败的其他原因是什么?如何检查?
取消链接源代码
is_writable
包含一个file_exists
检查。但是您必须检查包含的目录是否也是可写的。这应该做到:
if (is_writable($file) && is_writable(dirname($file))) {
unlink($file);
} else {
//...
}