/    Sign up×
Community /Pin to ProfileBookmark

sorting a directory listing

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>
<? } ?>
[/code]

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

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@jrthor2authorMar 11.2008 — I'm don't seemt o be getting the right date for the files. If I print out $date next to each file, the date for all of them is 2007-12-20, except for 1 of them, which doesn't make sense.
Copy linkTweet thisAlerts:
@jrthor2authorMar 11.2008 — Ok, I have this working now for all files in a directory, but how could I pass in how many items I'd like to see, and only return that number of files?
[code=php]
function sortfilesbydate($thedir) {
//first, you ensure that the directory exists.
if (is_dir($thedir)) {
//Now, you scan the files in this directory using scandir.
$scanarray = scandir($thedir);
$finalarray = array();
//Then you begin parsing the array.
//Since scandir() counts the "." and ".." file navigation listings as files
//you should not list them.
for ($i=0; $i < count($scanarray); $i++) {
if ($scanarray[$i] != "." && $scanarray[$i] != "..") {
//Now, you check to make sure this is a file, not a directory.
if (is_file($thedir . "/" . $scanarray[$i])) {
//Now what you need to do is cycle the data into an associative array.
$finalarray[$scanarray[$i]] = filectime($thedir . "/" . $scanarray[$i]);

}
}
}
//Finally, when you have gone through the entire array, you simply asort() it.
arsort($finalarray);
return ($finalarray);
} else {
echo "Sorry, this directory does not exist.";
}
}
[/code]

And I call it using:
[code=php]
$path = "documents/newsletters/";
$sortedarray = sortfilesbydate($path);
while ($element = each ($sortedarray)) {
$file = $element['key'];
$displayname = str_replace("_","",$file);
$date = date("F j, Y h:i:s",$element['value']);
?>
<li><a href="<?=str_replace(" ","%20",$path.$file);?>" title="<?=$displayname;?>" rel="external"><?=$displayname;?></a></li>
<? } ?>
[/code]

Thanks
Copy linkTweet thisAlerts:
@jrthor2authorMar 11.2008 — Ok, I figured it out, but I have one other issue. If I want to add this to my left navigation, I have to set the path as this:

$path = "/home/zluthorg/public_html/publications/documents/newsletters/";

rather than just:

$path = "/publications/documents/newsletters/";

Any reason why?

Thanks
×

Success!

Help @jrthor2 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.18,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...