woocha Posted December 18, 2007 Share Posted December 18, 2007 OK I know the basics, I think?....I know I will need to use opendir() sort() and filemtime()....but I can even begin to get how they will work together..In truth this problem might be a bit over my head, but I really want to give it a shot, so any help would be greatly appreciated. I want to sort the files in a given directory by the date they were last modified. I think this is possible, but I have talked to anyone who has done it yet, so if anyone could help me out, that woiuld be great. Thanks Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 <?php foreach (glob("admin/*.php") as $filename) { $array[fileatime($filename)]=$filename; } ksort($array); echo '<pre>'; print_r($array); echo '</pre>'; ?> try Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Access time and mod time are different.... Also, what if by some weird chance, the time on two files is the same? I would do this: $files = array(); if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $files[$file] = filemtime($file); } closedir($dh); } asort($files); print_r($files); The only problem with that is that the file names could be messed up if they aren't value for a PHP array key.... You could work around that by creating a second array of names though, and map them by IDs..... Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 OOPS yah sorry haven't test the code i just type it in notepad Quote Link to comment Share on other sites More sharing options...
woocha Posted December 18, 2007 Author Share Posted December 18, 2007 Access time and mod time are different.... Also, what if by some weird chance, the time on two files is the same? I would do this: $files = array(); if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { $files[$file] = filemtime($file); } closedir($dh); } asort($files); print_r($files); The only problem with that is that the file names could be messed up if they aren't value for a PHP array key.... You could work around that by creating a second array of names though, and map them by IDs..... Thank you...I will test it, but it that really all there is to it?...I thought there would have to so much more. Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Yeah, it's pretty simple if you know the right functions ;p. (P.S. I'm not sure if glob or open/readdir is faster.... Logically, I would expect open/readdir to be faster, but ya never know.) Quote Link to comment Share on other sites More sharing options...
teng84 Posted December 18, 2007 Share Posted December 18, 2007 IMO glob is slower because it acts like regex it matches for a pattern Quote Link to comment Share on other sites More sharing options...
corbin Posted December 18, 2007 Share Posted December 18, 2007 Yeah, that's what I was thinking too.... I wonder how glob and opendir would compare if you checked the files names in PHP when using opendir.... Hmm... Depending on how the patten was, I think glob might win then. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.