到目前为止我有这个:
<?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";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)