本文介绍了删除文件然后目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我到目前为止有这个:
<?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;
}
?>
它删除每个文件,这很棒.但是,它不会删除目录.怎么了?
It deletes every file, which is great. However, it doesn't remove the directory. What's wrong with this?
推荐答案
您需要将rmdir退出循环.像这样:
You need to pull the rmdir out of the loop. Something like:
$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";
}
这篇关于删除文件然后目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!