PHP遍历指定目录下所有文件函数,可指定文件类型
/**
* 遍历获取目录下的指定类型的文件
* @param $path
* @param array $files
* @return array
*/
function getfiles( $path , &$files = array() )
{
if ( !is_dir( $path ) ) return null;
$handle = opendir( $path );
while ( false !== ( $file = readdir( $handle ) ) ) {
if ( $file != '.' && $file != '..' ) {
$path2 = $path . '/' . $file;
if ( is_dir( $path2 ) ) {
getfiles( $path2 , $files );
} else {
if ( preg_match( "/\.(gif|jpeg|jpg|png|bmp)$/i" , $file ) ) {
$files[] = $path2;
}
}
}
}
return $files;
}
