当前位置:编程学习 > php >>

php 目录遍历为数组

 



function listDirTree($dirName=null) {
    
if(empty($dirName))exit("IBFileSystem:directoryisempty.");
    
if(is_dir($dirName)) {
        
if($dh=opendir($dirName)) {
            
$tree=array();
            
while(($file=readdir($dh))!==false) {
                
if($file!="."&&$file!="..") {
                    
$filePath=$dirName."/".$file;
                    
if(is_dir($filePath)) {
                        
$tree[$file]=listDirTree($filePath);
                    } 
else {
                        
$tree[]=$file;
                    }
                }
            }
            
closedir($dh);
        } 
else {
            
exit("IBFileSystem:cannotopendirectory$dirName.");
        }
        
return$tree;
    } 
else {
        
exit("IBFileSystem:$dirNameisnotadirectory.");
    }
}


补充:Php教程,Php常用代码
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,