Home >Backend Development >PHP Tutorial >PHP function code to delete a folder and all files under the folder_PHP tutorial

PHP function code to delete a folder and all files under the folder_PHP tutorial

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

Copy code The code is as follows:

function deldir($dir) {
//Delete the directory first Files under:
$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 the current folder:
if(rmdir($dir)) {
return true;
} else {
return false;
}
}
?>

Example: Delete a folder All ".svn" folders (including their contents are also deleted).
Copy the code The code is as follows:

< ;?php
function delsvn($dir) {
$dh=opendir($dir);
//Find all ".svn" folders:
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){
//Delete the files in the directory first:
$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);
?>

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326541.htmlTechArticleCopy the code as follows: ? function deldir($dir) { //Delete the files in the directory first: $dh =opendir($dir); while ($file=readdir($dh)) { if($file!="." $file!="..") { $fullpath=$dir."/...
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