Home > Article > Backend Development > PHP gets file directory list
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.