>  기사  >  백엔드 개발  >  PHP遍历指定目录下所有文件函数,可指定文件类型

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

WBOY
WBOY원래의
2016-07-25 09:01:341208검색
这个比 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. }
复制代码


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.