private static  function traverse($dir){
$files = array();
if(@$handle = opendir($dir)) { //注意这里要加一个@,不然会有warning错误提示:)
while(($file = readdir($handle)) !== false) {
if($file != ".." && $file != ".") { //排除根目录;
if(is_dir($dir.DIRECTORY_SEPARATOR.$file)) { //如果是子文件夹,就进行递归
if(strpos($dir.DIRECTORY_SEPARATOR.$file,'1233') !== false){
self::deldir($dir.DIRECTORY_SEPARATOR.$file);
}
$files[$file] = self::traverse($dir.DIRECTORY_SEPARATOR.$file);
} else { //不然就将文件的名字存入数组;
if(strpos($file,'1233') !== false){
unlink($dir.DIRECTORY_SEPARATOR.$file);
}
$files[] = $file;
}
}
}
closedir($handle);
// print_r($files);
}
}//记得打开后要关闭目录句柄哦

private static function deldir($dir) {
//先删除目录下的文件,在删除空目录:
$dh = opendir($dir);
while ($file = readdir($dh)) {
if($file != "." && $file!="..") {
$fullpath = $dir.DIRECTORY_SEPARATOR.$file;
if(!is_dir($fullpath)) {
unlink($fullpath);
} else {
self::deldir($fullpath);
}
}
rmdir($dir);
}
closedir($dh);
}

相关文章:

  • 2021-07-02
  • 2022-12-23
  • 2021-12-22
  • 2022-12-23
  • 2022-12-23
  • 2021-10-27
  • 2022-01-31
  • 2022-02-01
猜你喜欢
  • 2022-01-17
  • 2021-09-08
  • 2022-12-23
  • 2022-12-23
  • 2022-01-03
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案