Home  >  Article  >  Backend Development  >  Share: 4 ways to traverse files in PHP

Share: 4 ways to traverse files in PHP

WBOY
WBOYOriginal
2016-07-25 08:57:37793browse
  1. /**

  2. * Get all files in the current directory and subdirectories
  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

Copy code

Method 2, use glob The glob() function finds all file paths that match pattern according to the rules used by the libc glob() function, similar to the rules used by ordinary shells. No abbreviation expansion or parameter substitution is performed. Returns an array containing matching files/directories. Returns FALSE if an error occurs. This function does not work on remote files; the file being checked must be accessed through the server's file system. This function is used to search for files in a certain directory, and it can be called an artifact.

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

Copy code

Method 3, use directory class Fake object-oriented mechanisms for reading a directory. The dir() function opens a directory handle and returns an object. This object contains three methods: read(), rewind() and close(). And there are two properties available. The handle attribute can be used in other directory functions such as readdir(), rewinddir() and closedir(). The path attribute is set to the path of the directory being opened. If successful, the function returns a directory stream, otherwise it returns false and an error. You can hide error output by prepending "@" to the function name. Note: The order of directory entries returned by the read method is system dependent. Note: This function defines the internal class Directory, which means that the user's own class cannot be defined with the same name.

Example:

  1. /**
  2. * Recursively display all files in the currently 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. < ;p>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, use RecursiveDirectoryIterator class This method is valid since 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


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn