脚本宝典收集整理的这篇文章主要介绍了使用PHP删除空子文件夹,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
这是迄今为止开发的代码:
function RemoveEmptySubFolders($starting_From_path) { // Returns true if the folder contains no files function ISEMptyFolder($folder) { return (count(array_diff(glob($folder.DIRECTORY_SEPARATOR."*"),Array(".",".."))) == 0); } // Cycles thorugh the subfolders of $from_path and // returns true if at least one empty folder has been removed function DoRemoveEmptyFolders($from_path) { if(IsEmptyFolder($from_path)) { rmdir($from_path); return true; } else { $Dirs = glob($from_path.DIRECTORY_SEPARATOR."*",GLOB_ONLYDIR); $ret = false; foreach($Dirs as $path) { $res = DoRemoveEmptyFolders($path); $ret = $ret ? $ret : $res; } return $ret; } } while (DoRemoveEmptyFolders($starting_from_path)) {} }
function RemoveEmptySubFolders($path) { $empty=true; foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file) { if (is_dir($file)) { if (!RemoveEmptySubFolders($file)) $empty=false; } else { $empty=false; } } if ($empty) rmdir($path); return $empty; }
顺便说一句,glob不会返回.和..条目.
更短的版本:
function RemoveEmptySubFolders($path) { $empty=true; foreach (glob($path.DIRECTORY_SEPARATOR."*") as $file) { $empty &= is_dir($file) && RemoveEmptySubFolders($file); } return $empty && rmdir($path); }
以上是脚本宝典为你收集整理的使用PHP删除空子文件夹全部内容,希望文章能够帮你解决使用PHP删除空子文件夹所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。