php遍曆一個檔案夾下的所有目錄及檔案_PHP教程

來源:互聯網
上載者:User
  在面試中我們經常遇到這個題目:php遍曆一個檔案夾下的所有檔案和子檔案夾。

  這個題目有好多種解決方案。但大致思路都一樣。採用遞迴。

 
  1. $path = ./filepath;
  2. function getfiles($path)
  3. {
  4. if(!is_dir($path)) return;
  5. $handle = opendir($path);
  6. while( false !== ($file = readdir($handle)))
  7. {
  8. if($file != . && $file!=..)
  9. {
  10. $path2= $path./.$file;
  11. if(is_dir($path2))
  12. {
  13. echo
  14. ;
  15. echo $file;
  16. getfiles($path2);
  17. }else
  18. {
  19. echo
  20. ;
  21. echo $file;
  22. }
  23. }
  24. }
  25. }
  26. print_r( getfiles($path));
  27. echo
  28. ;
  29. function getdir($path)
  30. {
  31. if(!is_dir($path)) return;
  32. $handle = dir($path);
  33. while($file=$handle->read())
  34. {
  35. if($file!=. && $file!=..)
  36. {
  37. $path2 = $path./.$file;
  38. if(is_dir($path2))
  39. {
  40. echo $file."";
  41. getdir($path2);
  42. }else
  43. {
  44. echo $file.
  45. ;
  46. }
  47. }
  48. }
  49. }
  50. getdir($path);
  51. echo
  52. ;
  53. function get_dir_scandir(

    http://www.bkjia.com/PHPjc/486278.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/486278.htmlTechArticle在面試中我們經常遇到這個題目:php遍曆一個檔案夾下的所有檔案和子檔案夾。 這個題目有好多種解決方案。但大致思路都一樣。採用遞迴...

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.