我想从我的目录中删除多个文件,为此我使用以下代码
$x=array(".index.php",".code.html","about.txt");
foreach($x as $a)
unlink($a);
这段代码的有线之处在于它有时会起作用,有时候也不会,并且没有错误。
我有什么遗失的吗?
答案 0 :(得分:2)
为您的代码添加一些监控,以查看会发生什么:
foreach($x as $a) {
echo "File $a ";
if (file_exists($a)) {
if (is_file($a)) {
echo "is a regular file ";
} else {
echo "is not a regular file ";
}
if (is_link($a)) {
echo "is a symbolic link ";
}
if (is_readable($a)) {
echo " readable";
} else {
echo " NOT readable";
}
if (is_writeable($a)) {
echo " and writeable ";
} else {
echo " and NOT writeable ";
}
echo "owned by ";
echo posix_getpwuid(fileowner($a)) ['name'];
if (unlink($a)) {
echo "- was removed<br />\n";
} else {
echo "- was NOT removed<br />\n";
}
} else {
echo "doesn't exist<br />\n";
}
}
另请阅读this comment on the PHP manual page有关取消关联文件的信息。
如果您必须使用文件路径,请将其转换为功能realpath()
的实际路径 - 请参阅https://php.net/manual/en/function.realpath.php