[函数] 目录遍历和计算文件个数的简单函数
简介:
function get_files($path, $absolute=1) { $files = array(); $_path = realpath($path); if (!file_exists($_path)) return false;...
- function get_files($path, $absolute=1) {
- $files = array();
- $_path = realpath($path);
- if (!file_exists($_path)) return false;
- if (is_dir($_path)) {
- $list = scandir($_path);
- foreach ($list as $v) {
- if ($v == '.' || $v == '..') continue;
- $_paths = $_path.'/'.$v;
- if (is_dir($_paths)) {
- //递归
- $files = array_merge($files, get_files($_paths,$absolute));
- } else {
- $files[] = $absolute>0 ? $_paths : $v;
- }
- }
- } else {
- if (!is_file($_path)) return false;
- $files[] = $_path;
- }
- return $files;
- }
-
- $a = get_files('./Caige/api');
- $b = get_files('./Caige/api', 0);
- echo '<pre>';
- var_dump($a);
- var_dump($b);
- echo count($a).'<br />'.count($b);