Home  >  Article  >  Backend Development  >  php 删除目录,该怎么解决

php 删除目录,该怎么解决

WBOY
WBOYOriginal
2016-06-13 12:01:251000browse

php 删除目录
php删除指定目录下面的所有空的后代目录

求代码 求私思路
------解决方案--------------------
思路就是遍历,然后判断文件数量和文件夹数量为空,则删除。

用shell就简单了

find 目录 -mindepth 1 -depth -empty -type d -exec rm -r {} \;


用php就复杂了
<br />function rmEmptyDir($spath){<br />    if($handle = opendir($spath)){<br />        while(($file=readdir($handle))!==false){<br />            if($file!='.' && $file!='..'){<br />                $curfile = $spath.'/'.$file;<br /><br />                if(is_dir($curfile)){ // dir<br />                    rmEmptyDir($curfile);<br />                    if(count(scandir($curfile))==2){ // 空目錄<br />                        rmdir($curfile);<br />                    }<br />                }<br />            }<br />        }<br />        closedir($handle);<br />    }<br />}<br /><br />$folder = '目標文件夾';<br /><br />rmEmptyDir($folder);<br />

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