Share: PHP 4 ways to traverse files

Source: Internet
Author: User
    1. /**

    2. * Get all files in current directory and sub-directory
    3. * @param string $dir path name
    4. * @return Array of paths for all files in array
    5. */
    6. function Get_files1 ($dir) {
    7. $files = Array ();

    8. if (!is_dir ($dir)) {

    9. return $files;
    10. }

    11. $handle = Opendir ($dir);

    12. if ($handle) {
    13. while (false!== ($file = Readdir ($handle))) {
    14. if ($file! = '. ' && $file! = ' ... ') {
    15. $filename = $dir. "/" . $file;
    16. if (Is_file ($filename)) {
    17. $files [] = $filename;
    18. }else {
    19. $files = Array_merge ($files, Get_files ($filename));
    20. }
    21. }
    22. }//End While
    23. Closedir ($handle);
    24. }
    25. return $files;
    26. }//End Function

Copy Code

Method 2, use the Globglob () function to find all file paths that match the pattern in accordance with the rules used by the libc glob () function, similar to the rules used for general shells. No abbreviation extensions or parameter overrides are performed. Returns an array that contains a matching file/directory. Returns FALSE if an error occurs. This function does not work on remote files, and the files being inspected must be accessed through the server's file system. This function is used to search for files in a directory, which is an artifact.

Example:

    1. /**

    2. * Get all the files in the current directory
    3. * @param string $dir path name
    4. * @return Array of paths for all files in array
    5. */
    6. function Get_files ($dir) {
    7. $dir = Realpath ($dir). "/";
    8. $files = Array ();

    9. if (!is_dir ($dir)) {

    10. return $files;
    11. }

    12. $pattern = $dir. "*";

    13. $file _arr = Glob ($pattern);

    14. foreach ($file _arr as $file) {

    15. if (Is_dir ($file)) {
    16. $temp = Get_files ($file);

    17. if (Is_array ($temp)) {

    18. $files = Array_merge ($files, $temp);
    19. }
    20. }else {
    21. $files [] = $file;
    22. }//End If
    23. }
    24. return $files;
    25. }//End Function
    26. ?>

Copy Code

Method 3, use the directory-based phishing object-oriented mechanism to read a directory. The Dir () function opens a directory handle and returns an object. This object consists of three methods: Read (), rewind (), and Close (). And there are two properties available. The handle property can be used in other directory functions such as Readdir (), Rewinddir (), and Closedir (). The Path property is set to the directory path that is opened. If successful, the function returns a directory stream, otherwise it returns false and an error. You can hide the output of the error by adding "@" before the function name. Note: The order of the catalog entries returned by the Read method depends on the system. Note: This function defines the internal class Directory, which means that the user's own class can no longer be defined with the same name.

Example:

    1. /**

    2. * Recursively displays all files in the current specified directory
    3. * Use the Dir function
    4. * @param string $dir directory Address
    5. * @return Array $files file list
    6. * @site bbs.it-home.org
    7. */
    8. function Get_files ($dir) {
    9. $files = Array ();

    10. if (!is_dir ($dir)) {

    11. return $files;
    12. }

    13. $d = Dir ($dir);

    14. while (false!== ($file = $d->read ())) {
    15. if ($file! = '. ' && $file! = ' ... ') {
    16. $filename = $dir. "/" . $file;

    17. if (Is_file ($filename)) {

    18. $files [] = $filename;
    19. }else {
    20. $files = Array_merge ($files, Get_files ($filename));
    21. }
    22. }
    23. }
    24. $d->close ();
    25. return $files;
    26. }

Copy Code

Method 4, using the Recursivedirectoryiterator class This method is valid from PHP 5.0

Example:

    1. /**

    2. * Use Recursivedirectoryiterator to traverse files and list all file paths
    3. * @param recursivedirectoryiterator $dir Specifies the Recursivedirectoryiterator instance of the directory
    4. * @return Array $files file list
    5. */
    6. function Get_files ($dir) {
    7. $files = Array ();

    8. for (; $dir->valid (); $dir->next ()) {

    9. if ($dir->isdir () &&! $dir->isdot ()) {
    10. if ($dir->haschildren ()) {
    11. $files = Array_merge ($files, Get_files ($dir->getchildren ()));
    12. };
    13. }else if ($dir->isfile ()) {
    14. $files [] = $dir->getpathname ();
    15. }
    16. }
    17. return $files;
    18. }

    19. $path = "/var/www";

    20. $dir = new Recursivedirectoryiterator ($path);
    21. Print_r (Get_files ($dir));

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.