1 目录结构:
微信截图_20190824174844.png
2 程序以及思路:
- 获取目标路径
- 循环目标数组
- 如果是文件保存到数组
- 如果是目录再次调用本方法
- 最后排序
<?php
// 递归
function getFileAndDirectoryNames($path)
{
$dpath = scandir($path);
$count = count($dpath);
$names = [];
if ($count == 0) return [];
for ($i = 0; $i < $count; $i++)
{
// 过滤
if ($dpath[$i] != '.' && $dpath[$i] != '..')
{
$directory = $path . "/" . $dpath[$i];
// 是文件
if (is_file($directory)) $names[] = $dpath[$i];
// 是目录
if (is_dir($directory)) $names[$dpath[$i]] = getFileAndDirectoryNames($directory);
}
}
return $names;
}
$file = getFileAndDirectoryNames("../phptest");
asort($file);
print_r($file);
3 结果
Array
(
[0] => error.txt
[1] => file.php
[2] => index.php
[3] => reg_9.php
[4] => test.php
[5] => test.txt
[6] => user.csv
[ext] => Array
(
[0] => php.ini
[test] => Array
(
[0] => test.php
)
[test1] => Array
(
)
)
)
[Finished in 0.1s]
网友评论