Yesterday I saw a post (chinaasp) asking how to delete the directory. It has always been possible before, but yesterday there was a problem. 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.
My this can only be used temporarily. If your directory does not have more than ten layers, there should be no problem~, but I am not familiar with recursion, so I can only do it
deltree($path);rmdir($path) to delete this directory. Can you directly delete this directory? ?
function deltree($pathdir)
{
echo $pathdir;//I used 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, it is just the original path + the directory name of its subordinate
deltree($pathdir.'/'.$a);
}
if(is_empty_dir($pathdir.'/'.$a))
{//If it is empty, delete it directly
rmdir($pathdir.'/'.$a);
}
}
}
$d->close();
echo "You must delete all files in the directory first";//I used it when debugging
}
}
function is_empty_dir($pathdir)
{//To determine whether the directory is empty, isn't my method very good? Just see if there are other things other than... 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;
}
Directory, so if there are a few more levels, there will be problems.
My this can only be used temporarily. If your directory does not have more than ten layers, there should be no problem~, but I am not familiar with recursion, so I can only do it
deltree($path);rmdir($path) to delete this directory. Can you directly delete this directory? ?
function deltree($pathdir)
{
echo $pathdir;//I used 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, it is just the original path + the directory name of its subordinate
deltree($pathdir.'/'.$a);
}
if(is_empty_dir($pathdir.'/'.$a))
{//If it is empty, delete it directly
rmdir($pathdir.'/'.$a);
}
}
}
$d->close();
echo "You must delete all files in the directory first";//I used it when debugging
}
}
function is_empty_dir($pathdir)
{//To determine whether the directory is empty, isn't my method very good? Just see if there are other things other than... 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;
}