Home  >  Article  >  Backend Development  >  PHP遍历指定目录下所有文件函数,可指定文件类型

PHP遍历指定目录下所有文件函数,可指定文件类型

WBOY
WBOYOriginal
2016-07-25 09:01:341121browse
这个比 scandir出来的好用
  1. /**
  2. * 遍历获取目录下的指定类型的文件
  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. }
复制代码


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