Php 使用foreach()函数从web目录中删除多个文件

Php 使用foreach()函数从web目录中删除多个文件,php,Php,我想从我的目录中删除多个文件,为此我使用以下代码 $x=array(".index.php",".code.html","about.txt"); foreach($x as $a) unlink($a); 这段代码的特点是它有时有效,有时无效,而且没有错误 有什么我遗漏的吗?在代码中添加一些监控,看看会发生什么: foreach($x as $a) { echo "File $a "; if (file_exists($a)) { if (is_file(

我想从我的目录中删除多个文件,为此我使用以下代码

$x=array(".index.php",".code.html","about.txt");
 foreach($x as $a)
 unlink($a);
这段代码的特点是它有时有效,有时无效,而且没有错误


有什么我遗漏的吗?

在代码中添加一些监控,看看会发生什么:

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";
    }
}
foreach($x作为$a){
回显“文件$a”;
如果(文件_存在($a)){
如果(is_文件($a)){
echo“是一个常规文件”;
}否则{
echo“不是常规文件”;
}
如果(是链接($a)){
“回声”是一个符号链接;
}
如果(是否可读($a)){
呼应“可读”;
}否则{
回声“不可读”;
}
如果(可写($a)){
回声“可写”;
}否则{
echo“且不可写”;
}
回声“拥有者”;
echo posix_getpwuid(文件所有者($a))['name'];
如果(取消链接($a)){
echo“-已被删除
\n”; }否则{ echo“-未被删除
\n”; } }否则{ echo“不存在
\n”; } }
还可以阅读有关取消文件链接的内容


如果必须为文件使用路径,请使用函数
realpath()
将其转换为实际路径-请参见为什么在文件名开头使用点?