php - PHP目录/文件树结构 - 排序结果
问题描述
我有创建文件夹、子文件夹和文件的树结构的脚本。一切都很完美,但结果是未排序的。
我想我需要将第一个循环添加到一个数组中,然后对其进行排序。但是我的每一次实验都失败了......
请问,问题怎么解决?
非常感谢。
function createDir($path = '/home/www/mydomain/www/') {
// I think I need an array here
if ($handle = opendir($path)) {
echo "<ul>";
$queue = array();
while (false !== ($file = readdir($handle))) {
if (is_dir($path.$file) && $file != '.' && $file !='..') {
printSubDir($file, $path, $queue);
}
else if ($file != '.' && $file !='..') {
$queue[] = $file;
}
}
printQueue($queue, $path);
echo "</ul>";
}
}
function printQueue($queue, $path) {
asort($queue); // I tried this - it sorts only files and sub-folders but no root-folders
foreach ($queue as $file) {
printFile($file, $path);
}
}
function printFile($file, $path) {
echo "<li><a href=\"".$path.$file."\">$file</a></li>";
}
function printSubDir($dir, $path) {
echo "<li><span class=\"toggle\">$dir</span>";
createDir($path.$dir."/");
echo "</li>";
}
createDir();
解决方案
推荐阅读
- python-3.x - 将数据框中的日期时间列转换为分钟
- aws-cloudformation-custom-resource - 用户获取存储到 Cloud Formation 中 Secrets Manager 的访问密钥和密钥
- python - 收集时间戳列
- heroku - Strapi:为什么每次我更新strapi-app并将其部署在heroku上时数据都会被删除
- c++ - operator== 的隐式类型转换
- go - 如何在 golang 中模拟 go-plugin 函数?
- javascript - 动态更改默认选择的选项
- reactjs - 在 React 中基于 2 个 JSON 文件动态填充下拉列表
- java - 我试图复制在新选项卡中打开但复制旧选项卡 url 的 URL。java - 如何复制新标签页url java selenium?
- sql - 根据前几个月在 Bigquery 上生成值