Home > Article > Backend Development > PHP function to clear multi-level directories
PHP custom function clears the contents of a directory and subdirectories. The code is simple and concise. Friends in need can refer to it.
The complete code is as follows. <?php //清空多层目录 function del_dir($dir){ { if (!$dir) { return ; } $cacheDir = $dir; $dh = opendir($cacheDir); while ( $file = readdir($dh) ) { if (($file == '.') || ($file == '..')) { continue; } if (file_exists( $cacheDir .'/'.$file)) { if(is_dir( $cacheDir .'/'.$file)){ del_dir($cacheDir .'/'.$file); }elseif (!unlink($cacheDir .'/'. $file)) { //删除完操作 } } } } } ?> |