Home  >  Article  >  Backend Development  >  PHP traverse directory

PHP traverse directory

WBOY
WBOYOriginal
2016-07-25 09:08:41746browse
Traverse all directories and files in the specified directory
  1. function list_dir($dir){
  2. static $result = array();
  3. if(is_dir($dir)){
  4. $file_dir = scandir($dir);// List files and directories in the specified path
  5. foreach($file_dir as $file){
  6. if($file == '.'||$file == '..'){
  7. continue;
  8. }elseif(is_dir( $dir.$file)){
  9. list_dir($dir.$file.'/');
  10. }else{
  11. array_push($result,$dir.$file);//Data pushed onto the stack
  12. }
  13. }
  14. }
  15. return $result;
  16. }
  17. $arr = list_dir('E:/wwwroot/test/');
  18. print_r($arr);
  19. ?>
Copy code


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