php遍历指定路径下所有目录与文件(示例)
时间:2021-07-01 10:21:17
帮助过:16人阅读
- /**
- * 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);
|