PHP recursively deletes the specified folder method:
Here is a description of the PHP recursive deletion of the specified folder method, the example summarizes two commonly used recursive delete folder skills, very practical value, the need for friends can refer to the
The example of this article summarizes two methods of PHP recursive deletion of the specified folder. Share to everyone for your reference. Specifically as follows:
Method One:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23-24 |
function Recursivedelete ($dir) {if ($handle = @opendir ($dir)) {while ($file = Readdir ($handle))!== false) {if ($file == ".") || ($file = = "..") {continue;} if (Is_dir ($dir. '/' . $file)) {//Call self to this directory Recursivedelete ($dir. '/' . $file); else {unlink ($dir. '/' . $file); Remove this file}} @closedir ($handle); RmDir ($dir); } } |
Method Two:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30-31 |
/* Custom Delete function, you can delete file and recursive delete folder * * Function My_del ($path) {if (Is_dir ($path)) {$file _list= scandir ($path); foreach ($file _lis T as $file) {if ($file!= '. ' && $file!= '. ') {My_del ($path. ') /'. $file); }} @rmdir ($path); This method does not need to determine whether the folder is empty,//because regardless of whether the folder is empty at the beginning, it is empty if it arrives here ($path);//These two places are best to use @ Block warning error, look at the annoyance}} $path = ' d :/Technical documents-copy '; folder to delete//if PHP file is not ANSI, but UTF-8 mode,//and the folder you want to delete contains kanji characters, you need transcoding//$path =iconv (' utf-8 ', ' gb2312 ', $path) before calling the function. My_del ($path); |
I hope this article will help you with your PHP program design.