<?php
$dir = "testFile";//假设当前页面同目录下有testFile文件夹
//方式1
$dirSource = opendir($dir);
while ($childFileName = readdir($dirSource)) {
    if (!strcmp($childFileName, ".") || !strcmp($childFileName, "..")) {//排除testFile文件夹下最开始的 "."和".."两个文件名
        continue;
    }
    echo $childFileName . "目录列表:<br/><br/>";
    $childFileDir = "$dir/" . $childFileName;//子文件相对当前执php文件的路径
    $childDirLogoStr = "--";//子文件标识符号
    findAllChildFileName($childFileDir, $childDirLogoStr);
}

//方式2,直接调用方法把路径传进去也可沥遍所有结构
//findAllChildFileName($dir, "--");

function findAllChildFileName($dir, $childDirLogoStr) {
    if (is_dir($dir)) {
        $dirSource = dir($dir);
        while ($childFileName = $dirSource->read()) {//和上面沥遍结果一样,只是方式有点变化
            if (!strcmp($childFileName, ".") || !strcmp($childFileName, "..")) {
                continue;
            }
            $childFileDir = $dir . "/" . $childFileName;
            if (is_file($childFileDir)) {
                echo $childDirLogoStr . $childFileName . ":文件<br/>";
            } else {
                echo $childDirLogoStr . $childFileName . ":目录<br/>";
                findAllChildFileName($childFileDir, "--" . $childDirLogoStr);
            }
        }
        $dirSource->close();
    }
}
?>

 

相关文章:

  • 2022-01-09
  • 2022-02-06
  • 2022-12-23
  • 2021-08-20
  • 2022-01-20
  • 2022-12-23
  • 2021-06-20
猜你喜欢
  • 2021-06-13
  • 2022-12-23
  • 2021-09-30
  • 2021-10-13
  • 2022-01-03
  • 2022-12-23
  • 2021-06-16
相关资源
相似解决方案