Home  >  Article  >  Backend Development  >  How to delete multi-level directories_PHP tutorial

How to delete multi-level directories_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 16:03:17740browse

Yesterday I saw a post (chinaasp) asking how to delete a directory. It has always been possible before, but something went wrong yesterday. It turned out that he just deleted the files at the lower level and then deleted the

directory. So if there are a few more levels, there will be question.

I can only make do with this temporarily. If your directory does not have more than ten levels, it should be fine~, but I am not familiar with recursion and can only do

deltree($ path);rmdir($path) to delete this directory. Is it possible to delete this directory directly with deltree($path);? ?

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  
                                                 '/'.$a) && ($a!='.') && ($a!='..'))
                                                                                                                                                                             /<..> {// If not, call itself, but it is the original path+his subordinates' directory name
Deltree ($ Pathdir. '/'. $ a) ;
                                                                                                                                                                 ;
                                                                                                                                                                  

}

}

function is_empty_dir($pathdir)
{//My method of judging whether the directory is empty is not very good, right? Just to see if there are other things besides . and .. that are not empty, does PHP provide any

function?
$d=opendir($pathdir);
$i=0;
while($a=readdir($d))
{
$i++;
}
closedir($d);
if($i>2){return false;}
else return true;
}





http://www.bkjia.com/PHPjc/316344.html

www.bkjia.com

true

http: //www.bkjia.com/PHPjc/316344.html

TechArticleYesterday I saw a post (chinaasp) asking how to delete a directory. It has always been possible before, but something went wrong yesterday. It turns out He just deletes the directory after deleting the files at the lower level, so if there are a few more levels...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn