Forum Moderators: coopster

Message Too Old, No Replies

multiple directory listings

         

surrealillusions

4:25 pm on May 24, 2010 (gmt 0)

10+ Year Member



Hi all,

Would like to know the best way of going about this:

Problem - list the most recently updated subfolder from within a list of folders.

So,

root folder 1
-folder 1
--sub folder 1
--sub folder 2
-folder 2
--sub folder A
--sub folder B

Theres more folders and sub folders, but I'd thought I'd keep it simple for here.

Basically, need to search through the subfolders within the folders, find out which one is the most recently updated.

Was thinking to search through each folder and get the latest from each one, put them into an array, and then sort that array in date order, and then echo out the correct array entry.

Is there a better way?

jatar_k

1:22 pm on May 25, 2010 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



that method is what came to mind for me as well, take a shot and see how well it works. I am not sure how well it scales.

Are those files managed in some way that might allow updating a db with filetimes or something similar?

surrealillusions

9:45 pm on May 25, 2010 (gmt 0)

10+ Year Member



Well, I got it working.

This block of code I used over and over for each folder, unless theres an easier way of doing it..

BUT

I used different variable names for each folder name, so to put them in an array towards the end of the script.

$foldername = glob( 'path/to/foldername/*' );
// 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', $foldername ),
SORT_NUMERIC,
SORT_DESC,
$foldername
);
// do whatever you want with $files
//print_r( $files );

$foldername1 = $foldername[0];

And then at the end:

$array = array($foldername1, $foldername2, $foldername3, $foldername4, $foldername5, $foldername6, $foldername7);

array_multisort(
array_map( 'filemtime', $array ),
SORT_NUMERIC,
SORT_ASSC,
$array
);


$latestpost = $array[0];
// do whatever you want with this variable. such as explode it to get various parts, like just the foldername rather than just the full path, order this array in a particular order...etc..

Is there a more efficient way to run through the list of folders or not?