PHP recursively gets the code for the files in the directory (including subdirectories)

Source: Internet
Author: User
    1. /**

    2. * Recursively get files in directories and subdirectories
    3. * by bbs.it-home.org
    4. */

    5. function Readfilefromdir ($dir) {

    6. if (!is_dir ($dir)) {
    7. return false;
    8. }
    9. Open Directory
    10. $handle = Opendir ($dir);
    11. while (($file = Readdir ($handle))!== false) {
    12. Exclude current directory and previous directory
    13. if ($file = = "." | | $file = = "..") {
    14. Continue
    15. }
    16. $file = $dir. Directory_separator. $file;
    17. If the file is printed, it is called recursively.
    18. if (Is_file ($file)) {
    19. Print $file. '
      ';
    20. } elseif (Is_dir ($file)) {
    21. Readfilefromdir ($file);
    22. }
    23. }
    24. }

Copy Code

Call Mode:

    1. $dir = '/var/www/test ';
    2. Readfilefromdir ($dir);
Copy Code

Check out the PHP manual and find a method Scandir can also be used, this method will get all the files in the single-level directory at once, stored in the array. This method is not applicable when there are many files in the directory.

  • 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.