/** * php 遍历目录与文件 * by bbs.it-home.org */ function scan_all($dir){ $temp = scandir($dir); if(is_array($temp) && count($temp)>2){ array_shift($temp); array_shift($temp); foreach($temp as $v){ $cur_dir=$dir.DIRECTORY_SEPARATOR.$v; if(is_dir($cur_dir)){ echo $v." =>"; echo ""; scan_all($cur_dir); echo " "; }else if(is_file($cur_dir)){ echo "FILE:".$v.""; }else{ echo 'err'; } } } } $dir = "D:/yourdir/"; scan_all($dir); 复制代码 方法2,DirectoryIterator: /** * php 遍历目录与文件 * by bbs.it-home.org */ function directoryiterator($path) { $iterator = new DirectoryIterator($path); foreach ($iterator as $fileinfo) { if(!$fileinfo->isDot()) { if($fileinfo->isDir()) { echo $fileinfo.'=>'; directoryiterator($path.DIRECTORY_SEPARATOR.$fileinfo); echo ' '; } else { echo $fileinfo->getFilename().''; } } } } $dir = "D:/yourdir/"; directoryiterator($dir); 复制代码