Home  >  Article  >  Backend Development  >  PHP gets file directory list

PHP gets file directory list

高洛峰
高洛峰Original
2016-10-21 11:07:541912browse

Because we are traversing a certain folder, we must pass the address parameter $path of a file directory

function getDirList($path = '.') {    
    $allDirList = scandir($path);    
    foreach($allDirList as $key=>$val){        
        if (is_dir($path.$val) && $val!="." &&$val!="..") {            
            $filterDirList[] = $val;        
        }    
    }    
    return  $filterDirList;
}


The above method is to traverse the directories and files in each directory again through a foreach loop.

scandir method:

If successful, it will return an array containing the file name, if it fails, it will return FALSE. If directory is not a directory, a Boolean value of FALSE is returned and an E_WARNING level error is generated.


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