Yesterday I saw a post (chinaasp) asking how to delete a directory. It was always possible before, but something went wrong yesterday. It turned out that he just deleted his subordinate files and then deleted them
Directory, so if there are a few more levels, there will be problems.
Mine can only be used temporarily. If your directory does not have more than ten levels, it should be no problem~, but I am not familiar with recursion and can only do it
Deltree($path);rmdir($path) can be used to delete this directory. Can it be done directly deltree($path); to delete this directory? ?
function deltree($pathdir)
{
echo $pathdir;//I use it when debugging
if(is_empty_dir($pathdir))//If it is empty
{
rmdir($pathdir);//Delete directly
}
else
{//Otherwise read this directory, except . and ..
$d=dir($pathdir);
while($a=$d->read())
if(is_file($pathdir.'/'.$a) && ($a!='.') && ($a!='..')){unlink($pathdir.'/'.$a); }
//If it is a file, delete it directly
If(is_dir($pathdir.'/'.$a) && ($a!='.') && ($a!='..'))
{//If it is a directory
If(!is_empty_dir($pathdir.'/'.$a))//Is it empty
{//If not, call itself, which is just the original path + its subordinate directory name
deltree($pathdir.'/'.$a);
}
If(is_empty_dir($pathdir.'/'.$a))
{//If it is empty, delete it directly
rmdir($pathdir.'/'.$a);
}
}
}
$d->close();
echo "All files in the directory must be deleted first";//I use it when debugging
}
}
function is_empty_dir($pathdir)
{//My method of judging whether the directory is empty is not very good, right? Just check if there are other things besides . and .. that are not empty. Does php give anything?
function?
$d=opendir($pathdir);
$i=0;
While($a=readdir($d))
{
$i++;
}
closedir($d);
if($i>2){return false;}
else return true;
}
The above introduces how to use PHP to delete multi-level directories, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.