Home  >  Article  >  Backend Development  >  Summary of three methods to delete directories using rmdir under PHP_PHP Tutorial

Summary of three methods to delete directories using rmdir under PHP_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 15:52:49936browse

1. Recursive method: Use recursion to delete layer by layer.

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); 🎜>


2. System call method



Copy code The code is as follows:
function del_dir($dir) { if(strtoupper(substr(PHP_OS, 0, 3)) == 'WIN') {  $str = "rmdir /s/q " .$dir ;
} else {
$str = "rm -Rf " . $dir;
}
}



3. Round-robin method



Copy code The code is as follows:

function deltree($pathdir)
{
echo $pathdir;//Used for 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 not, call itself, which is just the original path + its subordinate directory name ; 🎜> } 
      }                                                                                          } 
function is_empty_dir($pathdir)                                                                                                  a=readdir($d))                                                                  return true;
}






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

www.bkjia.com

true

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

TechArticle

1. Recursive method: Use recursion to delete layer by layer. Copy the code as follows: deleteDir($dir) { if(rmdir($dir)==falseis_dir($dir)){ if($dp=opendir($dir)){ while(($file=r...





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