本文实例讲述了php判断并删除空目录及空子目录的方法。分享给大家供大家参考。具体实现方法如下:
步骤如下:
1.遍历目录及子目录
2.使用 scandir 判断目录是否为空,为空则使用rmdir 删除。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 | <?php
function rm_empty_dir( $path ){
if ( is_dir ( $path ) && ( $handle = opendir( $path ))!==false){
while (( $file =readdir( $handle ))!==false){
if ( $file != '.' && $file != '..' ){
$curfile = $path . '/' . $file ;
if ( is_dir ( $curfile )){
rm_empty_dir( $curfile );
if ( count (scandir( $curfile ))==2){
rmdir ( $curfile );
}
}
}
}
closedir ( $handle );
}
}
$folder = '目标文件夹' ;
rm_empty_dir( $folder );
?>
|
Copy after login
使用 shell 则简单很多:
1 | find 目标文件夹 -mindepth 1 -depth - empty -type d - exec rm -r {} \;
|
Copy after login
希望本文所述对大家的php程序设计有所帮助。