/**
* 获取当前目录下指定类型文件列表.
*
* @param string $dir 目录
* @param string $type 文件类型,以|分隔
*
* @return array 文件列表
*/
function GetFilesInDir($dir, $type)
{
$files = array();
$dir = str_replace('\\', '/', $dir);
if (substr($dir, -1) !== '/') {
$dir .= '/';
}
if (!is_dir($dir)) {
return array();
}
if (function_exists('scandir')) {
foreach (scandir($dir) as $f) {
if ($f != "." && $f != ".." && is_file($dir . $f)) {
foreach (explode("|", $type) as $t) {
$t = '.' . $t;
$i = strlen($t);
if (substr($f, -$i, $i) == $t) {
$sortname = substr($f, 0, (strlen($f) - $i));
$files[$sortname] = $dir . $f;
break;
}
}
}
}
} else {
$handle = opendir($dir);
if ($handle) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_file($dir . $file)) {
foreach (explode("|", $type) as $t) {
$t = '.' . $t;
$i = strlen($t);
if (substr($file, -$i, $i) == $t) {
$sortname = substr($file, 0, (strlen($file) - $i));
$files[$sortname] = $dir . $file;
break;
}
}
}
}
}
closedir($handle);
}
}
return $files;
}
/**
* 获取目录下文件夹列表(递归).
*
* @param string $dir 目录
*
* @return array 文件夹列表(递归函数返回的是路径的全称,和非递归返回的有区别)
*/
function GetDirsInDir_Recursive($dir)
{
$dirs = array();
if (!file_exists($dir)) {
return array();
}
if (!is_dir($dir)) {
return array();
}
$dir = str_replace('\\', '/', $dir);
if (substr($dir, -1) !== '/') {
$dir .= '/';
}
if (function_exists('scandir')) {
foreach (scandir($dir, 0) as $d) {
if (is_dir($dir . $d)) {
if (($d != '.') && ($d != '..')) {
$array = GetDirsInDir($dir . $d);
if (count($array) > 0) {
foreach ($array as $key => $value) {
$dirs[] = $dir . $d . '/' . $value;
}
}
$dirs[] = $dir . $d;
}
}
}
} else {
$handle = opendir($dir);
if ($handle) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != "..") {
if (is_dir($dir . $file)) {
$array = GetDirsInDir($dir . $file);
if (count($array) > 0) {
foreach ($array as $key => $value) {
$dirs[] = $dir . $file . '/' . $value;
}
}
$dirs[] = $dir . $file;
}
}
}
closedir($handle);
}
}
return $dirs;
}
/**
* 获取目录下指定类型文件列表(递归)..
*
* @param string $dir 目录
* @param string $type 文件类型,以|分隔
*
* @return array 文件列表
*/
function GetFilesInDir_Recursive($dir, $type)
{
$dirs = GetDirsInDir_Recursive($dir);
$dirs[] = $dir;
$files = array();
foreach ($dirs as $key => $d) {
$f = GetFilesInDir($d, $type);
foreach ($f as $key2 => $value2) {
$files[] = $value2;
}
}
return $files;
}