I have a folder that has pdf files in it. I need to display the pdf’s in this folder, and sort them by the last modified date in descending order (so the most recent is listed first). Below is the code I am trying to use, but it is not sorting correctly.
[code=php]
<?
$path = “documents/newsletters/”;
$names = array();
$dates = array();
if ($handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
if (strlen($file) – strpos(strtoupper($file), ‘.PDF’) == 4) {
$names[] = $file;
$dates[] = filectime(“$path/$file”);
}
}
closedir($handle);
}
print_r($dates);
rsort($dates);
foreach ($dates as $key=>$value) {
$file = $names[$key];
$displayname = str_replace(“_”,” “,$file);
$date = date(‘Y-m-d’, $value);
?>
<li><a href=”<?=str_replace(” “,”%20″,$path.$file);?>” title=”<?=$displayname;?>” rel=”external”><?=$displayname;?></a></li>
<? } ?>
Could someone help me get this working? Also, I would need a way to only show the first 5 in this list. How could I do that as well?
Thanks