php deletes the function code for the folder and all the files under it

  • 2020-05-27 04:38:26
  • OfStack

 
<? 
function deldir($dir) { 
// First, delete the files in the directory:  
$dh=opendir($dir); 
while ($file=readdir($dh)) { 
if($file!="." && $file!="..") { 
$fullpath=$dir."/".$file; 
if(!is_dir($fullpath)) { 
unlink($fullpath); 
} else { 
deldir($fullpath); 
} 
} 
} 
closedir($dh); 
// Delete current folder:  
if(rmdir($dir)) { 
return true; 
} else { 
return false; 
} 
} 
?> 

Example: delete all ".svn "folders under a folder (including its contents).
 
<?php 
function delsvn($dir) { 
$dh=opendir($dir); 
// Find out all ".svn "   Folder:  
while ($file=readdir($dh)) { 
if($file!="." && $file!="..") { 
$fullpath=$dir."/".$file; 
if(is_dir($fullpath)) { 
if($file==".svn"){ 
delsvndir($fullpath); 
}else{ 
delsvn($fullpath); 
} 
} 
} 
} 
closedir($dh); 
} 
function delsvndir($svndir){ 
// First, delete the files in the directory:  
$dh=opendir($svndir); 
while($file=readdir($dh)){ 
if($file!="."&&$file!=".."){ 
$fullpath=$svndir."/".$file; 
if(is_dir($fullpath)){ 
delsvndir($fullpath); 
}else{ 
unlink($fullpath); 
} 
} 
} 
closedir($dh); 
// Delete directory folder  
if(rmdir($svndir)){ 
return true; 
}else{ 
return false; 
} 
} 

$dir=dirname(__FILE__); 
//echo $dir; 
delsvn($dir); 
?> 

Related articles: