code Example of PHP reading directory list and file list

Source: Internet
Author: User
  1. /**

  2. * Getdir () go to Folder List
  3. * GetFile () go to the file list below the corresponding folder
  4. * The difference between the two is that there is no "." Suffix file, the others are the same
  5. * Edit bbs.it-home.org
  6. */

  7. Gets the file directory list, which returns the array

  8. function Getdir ($dir) {
  9. $dirArray []=null;
  10. if (false! = ($handle = Opendir ($dir))) {
  11. $i = 0;
  12. while (false!== ($file = Readdir ($handle))) {
  13. Remove the ".", ".." and files with a ". xxx" suffix
  14. if ($file! = "." && $file! = "..." &&!strpos ($file, ".")) {
  15. $dirArray [$i]= $file;
  16. $i + +;
  17. }
  18. }
  19. Close handle
  20. Closedir ($handle);
  21. }
  22. return $dirArray;
  23. }

  24. Get a list of files

  25. function GetFile ($dir) {
  26. $fileArray []=null;
  27. if (false! = ($handle = Opendir ($dir))) {
  28. $i = 0;
  29. while (false!== ($file = Readdir ($handle))) {
  30. Remove the ".", ".." and files with a ". xxx" suffix
  31. if ($file! = "." && $file! = "..." &&strpos ($file, ".")) {
  32. $fileArray [$i]= "./imageroot/current/". $file;
  33. if ($i ==100) {
  34. Break
  35. }
  36. $i + +;
  37. }
  38. }
  39. Close handle
  40. Closedir ($handle);
  41. }
  42. return $fileArray;
  43. }

  44. Call Method Getdir ("./dir") ... can be an absolute path or a relative path

  45. ?>

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.