2013-08-06 99 views
0

使用下面的代码,我正在生成位于服务器上的目录中的pdf列表。我希望按日期排序结果,最近的最早的最早的结果。按日期排序文件列表

这是在行动:http://mt-spacehosting.com/fisheries/plans/northeast-multispecies/

<?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> 

任何帮助,将不胜感激。

回答

1

你可以使用PHP的glob功能和自定义排序功能是这样的:

<?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++; 
} 
+0

好吧,我得到了一个工作,但目录正在选项文本中列出。任何方式来删除? – mtuttle

+1

是的。使用: echo“$ i。”; – Jithesh

+0

我最终不得不编辑回声线来生成正确的链接url,但你让我在那里!回声“$我。<选项值='”。 home_url($ file)。“'>”。str_replace(dirname($ file)。'/','',$ file)。“”; – mtuttle

0

这会得到在路径上的所有文件/到/文件作为一个数组,然后排序文件的修改时间数组

$files = glob('path/to/files/*.*'); 
usort($files, function($a, $b) { 
    return filemtime($a) < filemtime($b); 
});