Home >Backend Development >PHP Tutorial >PHP function code summary for deleting non-empty directories_PHP tutorial

PHP function code summary for deleting non-empty directories_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:13:191034browse

With this small program, PHPer does not need to manually delete directory files on the computer. You can use this function when practicing PHP directory file operations. On this basis, you can also add browsing folder directories and then delete them.

Code 1:

Copy code The code is as follows:

function d_rmdir($dirname) { //Delete non-empty directory
if(!is_dir($dirname)) {
return false;
}
$handle = @opendir($ dirname);
while(($file = @readdir($handle)) !== false){
if($file != '.' && $file != '..'){
$dir = $dirname . '/' . $file;
is_dir($dir) ? d_rmdir($dir) : unlink($dir);
}
}
closedir($handle );
return rmdir($dirname) ;
}
if(d_rmdir("./temp"))
echo "succes";
else
echo "false";
?>


The second one is taken from the manual:)

Code two:
Copy code The code is as follows:

 
functionremove_directory($dir){
 if($handle =opendir("$dir")){
while(false!==($item=readdir($handle))){
if($item!="."&&$item!=". ."){
if(is_dir("$dir/$item")){
remove_directory("$dir/$item");
}else{
unlink("$dir/ $item");
 echo"removing$dir/$item
";
  }
 }
 }
closedir($handle);
rmdir($dir) ;
echo "removing$dir
";
 }
}

The third one is collected from codebit.cn, and the one in the manual is better

Code three:

Copy code The code is as follows:

functionremoveDir($ dirName)
{
if(!is_dir($dirName))
{
returnfalse;
}
$handle=@opendir($dirName);
while(( $file=@readdir($handle))!==false)
 {
 if($file!='.'&&$file!='..')
 {
  $dir =$dirName.'/'.$file;
 is_dir($dir)?removeDir($dir):@unlink($dir);
  }
 }
 closedir($handle);
returnrmdir($dirName);
}
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326565.htmlTechArticleWith this small program, PHPer does not need to manually delete directory files on the computer. When practicing PHP directory files You can use this function during operation. On this basis, you can also add browser...
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