Introduced several PHP uses the recursive deletion file implementation code, hoped to be helpful to the friend's PHP study.
Loop + recursion
<?php deltree ('./copy copy of duplicate copy of duplicate copy of duplicate copy of the re-piece of AAA '); function deltree ($pathdir) {//echo $pathdir. ' <br/> ';//When I debug the IF (Is_empty_dir ($pathdir))//If it is empty {rmdir ($pathdir);//directly Delete} else {//otherwise read this directory except. and. External $d =dir ($pathdir); while ($a = $d->read ())//Under Delete only $pathdir under {if (Is_file ($pathdir. ') /'. $a) && ($a! = '. ') && ($a! = ' ... ')) {unlink ($pathdir. ') /'. $a); If it is a file, delete}elseif directly (Is_dir ($pathdir. ' /'. $a) && ($a! = '. ') && ($a! = ' ... ')) If the directory {if (!is_empty_dir ($pathdir. ') /'. $a))//Is empty {deltree ($pathdir. ' /'. $a); If not, call itself}else {rmdir ($pathdir. ' /'. $a); If it is empty directly delete}}} $d->close (); echo "must first delete all files in the directory";//RmDir ($PATHDIR) used for debugging; }} function Is_empty_dir ($pathdir) {//To determine if the directory is empty, my method is not very good? Something else is not empty $d =opendir ($pathdir); $i = 0; while ($a =readdir ($d)) {$i + +;} closedir ($d); if ($i >2) {return false;} else return true; }?>
Recursive method
<?php header ("content-type:text/html; charset=gb2312 "); if (Deletedir (')/Copy copy duplicate copy duplicate copy copy copy copy duplicate copy piece re-copy of (AAA ')) echo "Delete succeeded"; function Deletedir ($dir) {if (@rmdir ($dir) ==false && is_dir ($dir))//deleted, go to delete all files {if ($DP = Opendir ($dir)) {W Hile (($file =readdir ($DP))! = False) {if ($file! = '. ' && $file! = ' ... ') {//echo $file = $dir. '/'. $file; Echo ' <br/> '; $file = $dir. '/'. $file; if (Is_dir ($file))//is the real directory {Deletedir ($file);} else {unlink ($file);}} } closedir ($DP); }else {return false;}} if (Is_dir ($dir) && @rmdir ($dir) ==false)//is the directory is not deleted return false; return true; }?>
Two more recursion methods are introduced:
function Recursivedelete ($dir) { if ($handle = @opendir ($dir)) {while (($file = Readdir ($handle))!== false { if ($file = = ") | | ($file = = "..")) { continue; } if (Is_dir ($dir. '/' . $file)) { //call-self-for-this directory recursivedelete ($dir. '/' . $file); } else { unlink ($dir. '/' . $file); Remove this file } } @closedir ($handle); RmDir ($dir); }}
/* Custom Delete function, can delete file and recursively delete folder */function My_del ($path) {if (Is_dir ($path)) { $file _list= scandir ($path); foreach ($file _list as $file) { if ($file! = '. ' && $file! = ') { My_del ($path. ') /'. $file); } } @rmdir ($path); This method does not have to determine whether the folder is empty, //Because the folder is empty at the beginning, when it arrives here, it is empty } else { @unlink ($path); These two places best still need to use @ Shield warning error, look at the annoyance}} $path = ' d:/technical documents-copy ';//the folder to be deleted//if the PHP file is not ANSI, but UTF-8 mode,//and the folder to be deleted contains Chinese characters, Transcoding required before calling function//$path =iconv (' utf-8 ', ' gb2312 ', $path); My_del ($path);