Share: PHP file traversal methods-php Tutorial

Source: Internet
Author: User
Tags glob
Share: PHP file traversal methods

  1. /**

  2. * Obtain all files in the current directory and subdirectory.
  3. * @ Param string $ dir path name
  4. * @ Return array path array of all files
  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

Method 2: Use the globglob () function to search for all file paths that match the pattern according to the rules used by the libc glob () function, similar to the rules used by common shells. Do not perform abbreviation extension or parameter substitution. Returns an array containing matching files/directories. If an error occurs, FALSE is returned. This function cannot act on remote files. the checked files must be accessed through the file system of the server. This function is used to search for files in a directory.

Example:

  1. /**

  2. * Get all files in the current directory
  3. * @ Param string $ dir path name
  4. * @ Return array path array of all files
  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. ?>

Method 3: Use the directory class counterfeit object-oriented mechanism to read a directory. The dir () function opens a directory handle and returns an object. This object contains three methods: read (), rewind (), and close (). Two attributes are available. The handle attribute can be used in other Directory functions, such as readdir (), rewinddir (), and closedir. The path attribute is set to the opened directory path. If the operation succeeds, the function returns a directory stream. otherwise, false and an error are returned. You can add "@" before the function name to hide the error output. Note: the order of directory items returned by the read method depends on the system. Note: This function defines the internal class Directory, meaning that you cannot use the same name to define your own class.

Example:

  1. /**

  2. * Recursively display all files in the 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. }

Method 4: Use the RecursiveDirectoryIterator class. this method is effective from PHP 5.0.

Example:

  1. /**

  2. * Use RecursiveDirectoryIterator to traverse files and list all file paths
  3. * @ Param RecursiveDirectoryIterator $ dir specifies the directory's RecursiveDirectoryIterator instance
  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 ));

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.