社区微信群开通啦,扫一扫抢先加入社区官方微信群
社区微信群
<?php
function treeDir($pathName)
{
//将结果保存在result变量中
$result = array();
$temp = array();
//判断传入的变量是否是目录并判断文件是否可读
if(!is_dir($pathName) || !is_readable($pathName)) {
return null;
}
//取出目录中的文件和子目录名,使用scandir函数
$allFiles = scandir($pathName);
//遍历他们
foreach($allFiles as $fileName) {
//判断是否是.和..
if(in_array($fileName, array('.', '..'))) {
continue;
}
//路径加文件名
$filePath = $pathName.'/'.$fileName;
//如果是目录的话就继续遍历这个目录
if(is_dir($filePath)) {
//将这个目录中的文件信息存入到数组中
$result[$fileName] = treeDir($filePath);
//记录目录路径
$result[$fileName]['0']=$filePath;
}else {
$result[$fileName] = $filePath;
}
}
return $result;
}
$dir = isset($_GET['dir'])?$_GET['dir']:".";
//初始化函数
$tree = treeDir($dir );
echo "<pre>";
print_r($tree);
echo "</pre>";
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>文件遍历</title>
</head>
<body>
<h1>文件显示系统</h1>
<ul>
<?php foreach($tree as $k=>$v){
if(!is_array($v)){
echo "<span>$k</span><br />";
continue;
}
echo "<a href='?dir=$v[0]'>$k</a><br />";
}?>
</ul>
</body>
</html>
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!