The code is as follows |
Copy Code |
function Deltreedir ($dir) { $dir = Realpath ($dir); if (! $dir | |! @is_dir ($DIR)) return 0; $handle = @opendir ($dir); if ($dir [strlen ($dir)-1]!= directory_separator) $dir. = Directory_separator; while ($file = @readdir ($handle)) { if ($file!= '. ' && $file!= ' ... ') { if (@is_dir ($dir. $file) &&!is_link ($dir. $file)) Deltreedir ($dir. $file); Else @unlink ($dir. $file); } } Closedir ($handle); @rmdir ($dir); } |
Example Two
To recycle directory and file functions
The code is as follows |
Copy Code |
function Deldirandfile ($dirName) { if ($handle = Opendir ("$dirName")) { while (false!== ($item = Readdir ($handle))) { if ($item!= "." && $item!= "...") { if (Is_dir ("$dirName/$item")) { Deldirandfile ("$dirName/$item"); } else { if (unlink ("$dirName/$item")) echo "successfully deleted the file: $dirName/$item <br/>"; } } } Closedir ($handle); if (RmDir ($dirName)) echo "successfully deletes the directory: $dirName <br/>"; } } |
Example Three
The code is as follows |
Copy Code |
function Delfileunderdir ($dirName = ". /smarty/templates/templates_c ") { if ($handle = Opendir ("$dirName")) { while (false!== ($item = Readdir ($handle))) { if ($item!= "." && $item!= "...") { if (Is_dir ("$dirName/$item")) { Delfileunderdir ("$dirName/$item"); } else { if (unlink ("$dirName/$item")) echo "successfully deleted the file: $dirName/$item <br/>"; } } } Closedir ($handle); } } |
Deldirandfile (' www.111cn.net ');
Above three kinds of delete directories and directories under all the files and empty directory instances, are to traverse to do, that is, the return of a speech again to judge on OK.