Home > Backend Development > PHP Tutorial > PHP traverses all file functions in a specified directory and can specify the file type

PHP traverses all file functions in a specified directory and can specify the file type

WBOY
Release: 2016-07-25 09:01:34
Original
1265 people have browsed it
This is better than scandir is easy to use
  1. /**
  2. * Traverse to obtain files of the specified type in the directory
  3. * @param $path
  4. * @param array $files
  5. * @return array
  6. */
  7. function getfiles( $path , &$files = array() )
  8. {
  9. if ( !is_dir( $path ) ) return null;
  10. $handle = opendir( $path );
  11. while ( false !== ( $file = readdir( $handle ) ) ) {
  12. if ( $file != '.' && $file != '..' ) {
  13. $path2 = $path . ' /' . $file;
  14. if ( is_dir( $path2 ) ) {
  15. getfiles( $path2 , $files );
  16. } else {
  17. if ( preg_match( "/.(gif|jpeg|jpg|png|bmp)$ /i" , $file ) ) {
  18. $files[] = $path2;
  19. }
  20. }
  21. }
  22. }
  23. return $files;
  24. }
Copy code


source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template