-
27-09-2019 - |
题
我有这到目前为止:
<?php
$path = "files/";
$files = glob("" . $path . "{*.jpg, *.gif, *.png}", GLOB_BRACE);
$i = 0;
foreach($files as $file)
{
$delete = unlink($file);
if($delete)
{
echo $file . " deleted!<br />";
$i - 1;
}
else
{
echo $file . " could not be deleted...<br />";
$i + 1;
}
}
if($i == 0)
{
if(is_dir($path))
{
$remove = rmdir($path);
if($remove)
{
echo "directory was deleted</br />";
}
else
{
echo "directory could not be deleted</br />";
}
}
else
{
echo "not a valid directory<br />";
}
}
else
{
echo "there are some files in the folder";
echo $i;
}
?>
它删除的每一个文件,这是很大的。但是,它不会删除该目录。这有什么错呢?
解决方案
您需要拉命令rmdir圈外。是这样的:
$numfailed = 0;
foreach($files as $file)
{
$delete = unlink($file);
if($delete)
{
echo $file . " deleted!<br />";
}
else
{
echo $file . " could not be deleted...<br />";
$numfailed++;
}
}
if($numfailed == 0)
{
if(is_dir($path))
{
$remove = rmdir($path);
if($remove)
{
echo "directory was deleted</br />";
}
else
{
echo "directory could not be deleted</br />";
}
}
else
{
echo "not a valid directory<br />";
}
}
else
{
echo "there are still files in the folder, failed to remove $numfailed";
}
其他提示
您正在尝试删除目录中的foreach循环,将删除该目录内的文件。
我会尝试先删除所有文件,然后删除目录,否则将不能为空,不能删除。
另外你$ I-柜台不会做告诉你,当目录是空的工作:想象你的第一个文件将被删除,那么$ I = -1。如果现在你的第二个文件不会被删除,你的$ I = 0 ......这是上删除目录,即使它不是空的,因为至少你的第二个文件遗体。
RMDIR删除一个目录,但只有当它是空的。你要删除的每个文件(并与他们的文件,每个子目录)可以删除一个目录之前。
<强>可能的权限。强>
你只删除某些文件类型的某些可能存在的目录离开,你没有权限删除,有你无法删除该文件夹的其他文件。
尝试检查如果该文件夹是试图rmdir
命令上之前是空的。
不隶属于 StackOverflow