遍歷一個文件下的所有目錄和文件

這道題也是老題:遍歷文件下的所有目錄和文件

解答:

 

  1. <?php  
  2. function showPath($path)  
  3. {  
  4. $handle = opendir($path);  //打開傳遞過來的文件  
  5.  
  6. while($file = readdir($handle)){ //如果讀取文件不爲空  
  7.  
  8. if($file == "." || $file == ".."continue;  
  9.  
  10. $newFilePath = $path.DIRECTORY_SEPARATOR.$file;  
  11.  
  12. if(is_dir($newFilePath)){  
  13. echo "文件夾:".$newFilePath."<br>";  
  14. showPath($newFilePath);  
  15. }  
  16.  
  17. if(is_file($newFilePath)){  
  18. echo "文件:".$newFilePath;  
  19. }  
  20.  
  21. closedir($handle);  
  22.  }  
  23.  
  24. }  
  25.  
  26.  
  27. showPath('E:') ; 
  28.  
  29.  
  30.  
  31. ?> 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章