php - PHP - 按修改日期排序的目录列表
问题描述
这是我目前从(Apache)服务器获取文件列表并将它们排序为反向修改顺序的努力。
它完美无缺,但为什么反向排序不适用于目录列表?它很好地列出了它们,但没有订购它们。
<?php
function getFiles(){
$files=array();
if($dir=opendir('.')){
while($file=readdir($dir)){
if (!in_array($file,array(".","..","index.php"))) {
$files[]=$file;
}
}
closedir($dir);
}
rsort($files); ##sort reverse modified
return $files;
}
}
?>
解决方案
function getFiles()
{
$files = array();
//put files in an array both have same modified time
$atSameTimes = [];
if ($dir = opendir('.')) {
while ($file = readdir($dir)) {
if (!in_array($file, array(".", "..", "index.php"))) {
if (isset($files[filemtime($file)])) {
if (isset($atSameTimes[filemtime($file)]) && is_array($atSameTimes[filemtime($file)])) {
$atSameTimes[filemtime($file)][] = $file;
} else {
$atSameTimes[filemtime($file)][] = $files[filemtime($file)];
$atSameTimes[filemtime($file)][] = $file;
}
} else {
$files[filemtime($file)] = $file;
}
}
}
closedir($dir);
}
$arr = array_replace_recursive($files, $atSameTimes);
ksort($arr, SORT_NUMERIC); ##Sort an array in descending order and maintain index association
//asort($arr); ##Sort an array in ascending order and maintain index association
return $arr;
}
样本输出:
array(4) {
[1577562574]=>
array(3) {
[0]=>
string(15) "CONTRIBUTING.md"
[1]=>
string(9) "README.md"
[2]=>
string(11) "phpunit.xml"
}
[1607445739]=>
array(3) {
[0]=>
string(7) "LICENSE"
[1]=>
string(13) "composer.json"
[2]=>
string(12) "CHANGELOG.md"
}
[1612604942]=>
string(19) "LoginController.php"
[1622636168]=>
string(19) "StoreController.php"
}
推荐阅读
- scala - scala集成测试“没有这样的设置/任务”
- javascript - 使用变量作为对象键
- javascript - 具有自身功能的速度交错
- python-3.x - 使用 while 和 for 循环进行字符串压缩
- c# - 完成导出后多次显示“保存对话框”窗口
- git - 即使我添加,Git 提交也不起作用,它说“没有为提交暂存更改”
- c - 如何从进程中分离,以便它可以被另一个进程跟踪?
- python - 在 python 中,生成元组的最快方法是什么:(1.0, 0.0, 0.0, 2.0, 0.0, 0.0, ..., N, 0.0, 0.0)?
- c# - Windows Form App C#调用DbContext的正确方法
- python - 给定原始和卷积一维数据的核估计