按日期排序文件列表 [英] Sort file list by date

查看:34
本文介绍了按日期排序文件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我生成了位于服务器目录中的 pdf 列表.我想让结果按日期排序,最近的第一个最旧的最后一个.

With the code below I am generating a list of pdfs located in a directory on my server. I'd like to have the results sort by date, most recent first oldest last.

这是在行动:http://mt-spacehosting.com/fisheries/计划/东北多物种/

<?php 
$sub = ($_GET['dir']); 
$path = 'groundfish-meetings/';
$path = $path . "$sub"; 
$dh = opendir($path); 
$i=1; 

while (($file = readdir($dh)) !==   false) { 
    if($file != "." && $file != "..") { 
        if (substr($file, -4, -3) =="."){
         echo "$i. <option value='" . home_url('/groundfish-meetings/' . $file) .         "'>$file</option>";
         } $i++; 
        } 
     } closedir($dh); 
?>
</select>

任何帮助将不胜感激.

推荐答案

你可以使用 PHP 的 glob 函数和一个像这样的自定义排序函数:

You could use PHP's glob function and a custom sorting function like this:

<?php
$sub = ($_GET['dir']); 
$path = 'groundfish-meetings/';
$path = $path . "$sub";
$file_list = glob($path."*.pdf");

function sort_by_mtime($file1,$file2) {
$time1 = filemtime($file1);
$time2 = filemtime($file2);
if ($time1 == $time2) {
    return 0;
}
return ($time1 < $time2) ? 1 : -1;
}
usort($file_list ,"sort_by_mtime");
$i = 1;
foreach($file_list as $file)
{
  echo "$i. <option value='" . home_url('/groundfish-meetings/' . $file) .            
"'>$file</option>";
  $i++;
}

这篇关于按日期排序文件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆