现在默认情况下它按字母表显示,我不希望这样。我想使用以下方式对文件进行排序递归目录迭代器最新文件位于顶部。按降序排列。
还使用if 条件比较日期并获取该日期的文件
<?php
$search_path = 'D:\xampp\htdocs';
$file_extension = 'php';
$it = new RecursiveDirectoryIterator("$search_path");
$display = Array ($file_extension);
foreach(new RecursiveIteratorIterator($it) as $file) {
$test = Array();
$test = explode("/",date("m/d/Y",filemtime($file)));
$year = $test[2];
$day = $test[1];
$month = $test[0];
if (in_array(strtolower(array_pop(explode('.', $file))), $display))
if(($year >= 2014) && ($month >= 1) && ($day >= 15 )){
echo "<span style='color:red;'>";
echo "<b style='color:green;'>".$day.'-'.$month.'-'.$year. '</b> ' . $file."</span><br>";
}
}
?>
不知道你能否整理一下DirectoryIterator
直接来说,您可以先收集结果,获取时间,然后排序,然后呈现。例子:
$display = array('php');
$files = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($search_path));
$data = array();
foreach($files as $file) {
$time = DateTime::createFromFormat('U', filemtime($file->getPathname()));
// no need to explode the time, just make it a datetime object
if(in_array($file->getExtension(), $display) && $time > new DateTime('2014-01-15')) { // is PHP and is greater than jan 15 2014
$data[] = array('filename' => $file->getPathname(), 'time' => $time->getTimestamp()); // push inside
}
}
usort($data, function($a, $b){ // sort by time latest
return $b['time'] - $a['time'];
});
foreach ($data as $key => $value) {
$time = date('Y-m-d', $value['time']);
echo "
<span style='color: red;'>
<b style='color: green;'>$time</b>$value[filename]
</span>
<br/>
";
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)