Home > Backend Development > PHP Tutorial > Discuss three methods of deleting folders in PHP_PHP Tutorial

Discuss three methods of deleting folders in PHP_PHP Tutorial

WBOY
Release: 2016-07-21 15:07:52
Original
943 people have browsed it

1. Recursive method

Copy code The code is as follows:

deleteDir( $dir)
{
if (rmdir($dir)==false && is_dir($dir)) {
if ($dp = opendir($dir)) {
while (($ file=readdir($dp)) != false) {
if (is_dir($file) && $file!='.' && $file!='..') {
deleteDir($file) ;
} else {
unlink($file);
}
}
closedir($dp);
} else {
exit('Not permission');
}
}
}

2. System call method
Copy the code The code is as follows:

function del_dir($dir)
{
if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN ') {
                                                                                                                                                                🎜>



3. Round-robin method


Copy code
The code is as follows:
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);}
                                                                          ‐‐’'''' .$a) && ($a!='.') && ($a!='..'))
                                                                                                                                                                                         $a))//Is it empty? >}
If (IS_EMPTY_DIR ($ PATHDIR. '/'. $ A))
{// If it is empty, delete
rmdir ($ PATHDIR. '/'. $ A); }
}
}
$ d-& gt; close ();
Echo "must first delete all files in the directory"; //
} }
function is_empty_dir($pathdir)
{
//Judge whether the directory is empty, isn’t my method very good? Just see if there are other things besides . and .. that are not empty
$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/327525.htmlTechArticle1. The recursive copy code is as follows: deleteDir($dir) { if (rmdir($dir)== false is_dir($dir)) { if ($dp = opendir($dir)) { while (($file=readdir($dp)) != false) { if (is_dir($fil...
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