PHP Gets the implementation code for all directories and files contained in a directory

Source: Internet
Author: User
  1. /**
  2. * Get all directories and files included in the input directory
  3. * Returned as an associative array
  4. * edit:bbs.it-home.org
  5. */
  6. function Deepscandir ($dir)
  7. {
  8. $FILEARR = Array ();
  9. $DIRARR = Array ();
  10. $dir = RTrim ($dir, '//');
  11. if (Is_dir ($dir)) {
  12. $dirHandle = Opendir ($dir);
  13. while (false!== ($fileName = Readdir ($dirHandle))) {
  14. $subFile = $dir. Directory_separator. $fileName;
  15. if (Is_file ($subFile)) {
  16. $FILEARR [] = $subFile;
  17. } elseif (Is_dir ($subFile) && str_replace ('. ', ', $fileName)! = ') {
  18. $DIRARR [] = $subFile;
  19. $arr = Deepscandir ($subFile);
  20. $DIRARR = Array_merge ($DIRARR, $arr [' dir ']);
  21. $FILEARR = Array_merge ($FILEARR, $arr [' file ']);
  22. }
  23. }
  24. Closedir ($dirHandle);
  25. }
  26. Return Array (' dir ' = = $dirArr, ' file ' = $FILEARR);
  27. }
  28. Example
  29. $dir = '/var/htdocs/w4/article ';
  30. $arr = Deepscandir ($dir);
  31. Print_r ($arr);
  32. /**
  33. * Get all the files contained in the input directory
  34. * Returned as an array
  35. * AUTHOR:FLYNETCN
  36. */
  37. function Get_dir_files ($dir)
  38. {
  39. if (Is_file ($dir)) {
  40. Return Array ($dir);
  41. }
  42. $files = Array ();
  43. if (Is_dir ($dir) && ($dir _p = Opendir ($dir))) {
  44. $ds = Directory_separator;
  45. while ($filename = Readdir ($dir _p))!== false) {
  46. if ($filename = = '. ' | | $filename = = ' ... ') {continue;}
  47. $filetype = filetype ($dir. $ds. $filename);
  48. if ($filetype = = ' dir ') {
  49. $files = Array_merge ($files, Get_dir_files ($dir. $ds. $filename));
  50. } elseif ($filetype = = ' file ') {
  51. $files [] = $dir. $ds. $filename;
  52. }
  53. }
  54. Closedir ($dir _p);
  55. }
  56. return $files;
  57. }
Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.