Computing.Net > Forums > Web Development > PHP Sort directory contents by date

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP Sort directory contents by date

Reply to Message Icon

Name: RTAdams89
Date: December 19, 2007 at 16:39:18 Pacific
OS: na
CPU/Ram: na
Product: na
Comment:

I am using the following PHP script to list the contents on a folder called "minutes."

<?php
if ($handle = opendir('./minutes/')) {
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$thelist .= ''.$file.'
';
}
}
closedir($handle);
}
?>

Is there a simple way to have the list that is returned sorted by the date the file was modified?

-Ryan Adams
http://RyanTAdams.com



Sponsored Link
Ads by Google

Response Number 1
Name: RTAdams89
Date: December 19, 2007 at 18:28:48 Pacific
Reply:

Got it, nevermind

-Ryan Adams
http://RyanTAdams.com


0

Response Number 2
Name: kbluhm
Date: April 18, 2008 at 01:26:53 Pacific
Reply:

Here is a solution that, based on the archaic PHP3-style example code, is most likely better than what the OP came up with. ;)


// Grab all files from the desired folder
$files = glob( './minutes/*.*' );

// Sort files by modified time, latest to earliest
// Use SORT_ASC in place of SORT_DESC for earliest to latest
array_multisort(
array_map( 'filemtime', $files ),
SORT_NUMERIC,
SORT_DESC,
$files
);

// do whatever you want with $files
print_r( $files );



0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More







Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP Sort directory contents by date

How to sort found web-sites (Google) by date www.computing.net/answers/webdevel/how-to-sort-found-websites-google-by-date/4061.html

Alpha sorting with PHP & MySQL www.computing.net/answers/webdevel/alpha-sorting-with-php-amp-mysql/304.html

PHP Sub directories www.computing.net/answers/webdevel/php-sub-directories/3584.html