使用RecursiveDirectoryIterator按日期排序文件 [英] Sort files by date latest at top Using RecursiveDirectoryIterator
本文介绍了使用RecursiveDirectoryIterator按日期排序文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如果条件比较日期&从该日期获得文件
<?php
$ search_path ='D:\xampp \htdocs';
$ file_extension ='php';
$ it = new RecursiveDirectoryIterator($ search_path);
$ display = Array($ file_extension);
foreach(新的RecursiveIteratorIterator($ it)作为$ 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个'。 $文件 < /跨度><峰; 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()));
//不需要爆炸时间,只要将它设为日期时间对象
if(in_array($ file-> getExtension(),$ display)&& $ amp; time> new DateTime '2014-01-15')){//是PHP并且大于jan 15 2014
$ data [] = array('filename'=> $ file-> getPathname(),'time' => $ time-> getTimestamp()); ($ a
$ b $ {$,$ b){//按时间最新
返回$ 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 />
;
}
Right now by default its display by alphabet i don't want that way. I Want to sort files by using RecursiveDirectoryIterator latest files at top. In descending order.
Also use if condition to compare date & get files from that date
<?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>";
}
}
?>
解决方案
I don't know if you can sort thru the DirectoryIterator
outright by you could gather the results first, get the time then sort that then present it. Example:
$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/>
";
}
这篇关于使用RecursiveDirectoryIterator按日期排序文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文