Home > Backend Development > PHP Tutorial > How to delete multi-level directories in php_PHP tutorial

How to delete multi-level directories in php_PHP tutorial

WBOY
Release: 2016-07-13 17:23:32
Original
1246 people have browsed it

Yesterday I saw a post (chinaasp) asking how to delete a directory. It has always been possible before, but yesterday something went wrong. 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
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";//The

} I use when debugging



}

function is_empty_dir($pathdir)
{//Judge whether the directory is empty, my method 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;
}

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532205.htmlTechArticleYesterday 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...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template