php 手册学习 文件系统之 循环遍历目录下所有文件

 递归遍历目录下所有文件
1
function getfile($dir){ 2 $files = array(); 3 if(is_dir($dir)){ 4 if($dh = opendir($dir)){ 5 while(false !== ($file = readdir($dh))){ 6 if($file != "." && $file != ".."){ 7 $file_a = ($dir."/".$file); 8 if(is_dir($file_a)){ 9 // echo $file_a. '<br>'; 10 $files['dir'][$file_a] = getfile($file_a); 11 }else{ 12 if(is_file($file_a)){ 13 // echo $file_a. '<br>'; 14 $files["file"][] = $file_a; 15 } 16 } 17 } 18 } 19 closedir($dh); 20 } 21 } 22 return $files; 23 } 24 $dir = getcwd(); 25 var_dump(getfile($dir));
 1 只读取一级目录下文件
 2 
 3 function dirList($dir_path = '') {
 4     if(is_dir($dir_path)) {
 5         $dirs = opendir($dir_path);
 6         if($dirs) {
 7             while(($file = readdir($dirs)) !== false) {
 8                 if($file !== '.' && $file !== '..') {
 9                     if(is_dir($file)) {
10                         echo $dir_path . '/' . $file . '<br>';
11                         dirList($dir_path . '/' . $file);
12                     } else {
13                         echo $dir_path . '/' . $file . '<br>';
14                     }
15                 }
16             }
17             closedir($dirs);
18         }
19     } else {
20         echo '目录不存在!';
21     }
原文地址:https://www.cnblogs.com/gaogaoxingxing/p/11152464.html