Home  >  Article  >  Backend Development  >  PHP function to clear multi-level directories

PHP function to clear multi-level directories

WBOY
WBOYOriginal
2016-07-25 09:00:29737browse
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)) {
//删除完操作
}
}
}
}
}
?>


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