如何遍历一个目录下的所有子目录和文件?

PHP 算法面试题
1
0
分享
推荐答案
展示答案

<?php //遍历所有子目录和文件 $items = scan_dir('../'); print_r($items); function scan_dir($dir) {     $files = array();     if (is_dir($dir)) {         //打开目录         if ($dh = opendir($dir)) {             //遍历目录             while (($file = readdir($dh)) !== false) {                 //子文件或子目录                 $dist = $dir . $file;                 if (is_dir($dist)) {                     //如果是目录,则进行递归遍历                     if ($file != '.' && $file != '..') {                         //去掉 . 和 ..                         //遍历子目录                         $files[$file] = scan_dir($dist);                     }                 } else {                     //保存子文件                     $files[] = $file;                 }             }             //关闭目录             closedir($dh);         }     }     return $files; }

答案已隐藏