/**
* 遍历获取目录下的指定类型的文件
* @param $path
* @param array $files
* @return array
*
* -- Demo --
* dump($_SERVER['DOCUMENT_ROOT'] . '/' . trim(C('UPLOAD_COMM_PATH'), '/') . '/sucai/');
* $allowFiles = array('.png', '.jpg', '.jpeg', '.bmp');
* $allowFiles = substr(str_replace(".", "|", join("", $allowFiles)), 1);
* $files = getfiles($_SERVER['DOCUMENT_ROOT'] . '/' . trim(C('UPLOAD_COMM_PATH'), '/') . '/sucai/', $allowFiles);
* foreach ($files as $file) {
*
echo '<img src="' . $file['url'] . '">';
* }
*
*/
function getfiles($path, $allowFiles, &$files = array())
{
if (!is_dir($path)) return null;
if(substr($path, strlen($path) - 1) != '/') $path .= '/';
$handle = opendir($path);
while (false !== ($file = readdir($handle))) {
if ($file != '.' && $file != '..') {
$path2 = $path . $file;
if (is_dir($path2)) {
getfiles($path2, $allowFiles, $files);
} else {
if (preg_match("/\.(".$allowFiles.")$/i", $file)) {
$files[] = array(
'url'=> substr($path2, strlen($_SERVER['DOCUMENT_ROOT'])),
'mtime'=> filemtime($path2)
);
}
}
}
}
return $files;
}