Home > Article > Backend Development > How to traverse folders and file lists in php PHP directory traversal example code
Share several PHP codes for traversing directories and files, a PHP class for traversing directories and file lists, and provide a calling instance of this PHP directory class, which can be used to obtain a list of directories and files under relative paths or absolute paths.
How to traverse folder and file list in php? PHP class for traversing directories and file lists, with usage examples. Code: Copy code Code example: getDir( $Dir ); if( $DirFileArray ){ foreach( $DirFileArray['DirList'] as $Handle ){ $File = $Dir.DS.$Handle; $DirFileArray['FileList'][$Handle] = $this->getFile( $File ); } } }else{ $DirFileArray[] = '[Path]:''.$Dir.'' is not a dir or not found!'; } return $DirFileArray; } } ?>Example: (relative path or absolute path) 1, php gets directory list Copy code Code example: getDir( $Dir_dir ); print_r($getDir); ?>2, php gets file list Copy code Code example: getFile( $File_one_dir ); $getFile_two = $getDirFile->getFile( $File_two_dir ); print_r($getFile_one); print_r($getFile_two); ?>3, php gets directory/file list Copy code Code example: getDirFile( $Dir_dir ); print_r($getDirFile); ?> |