PHP function code summary for deleting non-empty directories_PHP tutorial

WBOY
Release: 2016-07-21 15:13:19
Original
996 people have browsed it

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...
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!