aMies Posted January 8, 2009 Share Posted January 8, 2009 I am currently using a script to echo out images in a folder, using the thumbs as a display, linking to the fullsize image. How would I go about sorting them by the most recentely added? This is for a php upload filesystem. <?php $featured_dir = 'images/'; $dir = 'thumbs/'; $scan = scandir($dir); for ($i = 0; $i<count($scan); $i++) { if ($scan[$i] != '.' && $scan[$i] != '..') { if (strpos($scan[$i], '.jpg') !== false) { echo ' <div id="imageList"> <ul><a href="' . $featured_dir . $scan[$i] . '"> <img src="' . $dir . $scan[$i] . '" alt="' . $scan[$i] . '" /> </a>'; echo '' . $scan[$i] .''; echo '</div>'; } } }; ?> Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/ Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 You'll have to read the files into an array along with their modification times (obtained using filemtime) and sort on those times. I'd probably use the name as the index of the array and the modification time as the values so sorting is simply a case of a call to rsort Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732472 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 You would use the filemtime function and most likely put the results in an array using that as a key, then do a ksort on the array and then a foreach to iterate through and print the images back... <?php $featured_dir = 'images/'; $dir = 'thumbs/'; $scan = scandir($dir); for ($i = 0; $i<count($scan); $i++) { if ($scan[$i] != '.' && $scan[$i] != '..') { if (strpos($scan[$i], '.jpg') !== false) { $fileAr[filemtime($dir . $scan[$i]]['dir'] = $dir; $fileAr[filemtime($dir . $scan[$i]]['filename'] = $scan[$i]; $fileAr[filemtime($dir . $scan[$i]]['path'] = $dir . $scan[$i]; } } } ksort($fileAr); foreach ($fileAr as $files) { echo ' <div id="imageList"> <ul><a href="' . $featured_dir . $files['filename'] . '"> <img src="' . $files['path'] . '" alt="' . $files['filename'] . '" /> </a>'; echo '' . $files['filename'] .''; echo '</div>'; } ?> If you want the oldest first, krsort instead. Untested, but I think the logic should work. Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732474 Share on other sites More sharing options...
aMies Posted January 8, 2009 Author Share Posted January 8, 2009 Yeah, I ended up writing it quite similar to what you wrote premiso, however I'm getting a Parse error on line 31 Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732483 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 Yeah, I ended up writing it quite similar to what you wrote premiso, however I'm getting a Parse error on line 31 If you want help to fix it, I would suggest posting the code and highling or make a notation where line 31 is. Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732485 Share on other sites More sharing options...
aMies Posted January 8, 2009 Author Share Posted January 8, 2009 You were just missing a ')' is all. <?php $featured_dir = 'images/'; $dir = 'thumbs/'; $scan = scandir($dir); for ($i = 0; $i<count($scan); $i++) { if ($scan[$i] != '.' && $scan[$i] != '..') { if (strpos($scan[$i], '.jpg') !== false) { $fileAr[filemtime($dir . $scan[$i])]['dir'] = $dir; $fileAr[filemtime($dir . $scan[$i])]['filename'] = $scan[$i]; $fileAr[filemtime($dir . $scan[$i])]['path'] = $dir . $scan[$i]; } } } ksort($fileAr); foreach ($fileAr as $files) { echo ' <div id="imageList"> <ul><a href="' . $featured_dir . $files['filename'] . '"> <img src="' . $files['path'] . '" alt="' . $files['filename'] . '" /> </a>'; echo '' . $files['filename'] .''; echo '</div>'; } ?> </div> Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732488 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 You were just missing a ')' is all. <?php $featured_dir = 'images/'; $dir = 'thumbs/'; $scan = scandir($dir); for ($i = 0; $i<count($scan); $i++) { if ($scan[$i] != '.' && $scan[$i] != '..') { if (strpos($scan[$i], '.jpg') !== false) { $fileAr[filemtime($dir . $scan[$i])]['dir'] = $dir; $fileAr[filemtime($dir . $scan[$i])]['filename'] = $scan[$i]; $fileAr[filemtime($dir . $scan[$i])]['path'] = $dir . $scan[$i]; } } } ksort($fileAr); foreach ($fileAr as $files) { echo ' <div id="imageList"> <ul><a href="' . $featured_dir . $files['filename'] . '"> <img src="' . $files['path'] . '" alt="' . $files['filename'] . '" /> </a>'; echo '' . $files['filename'] .''; echo '</div>'; } ?> </div> Oh, I thought you were using your code, hence the post it part =) But yea, that works. Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732492 Share on other sites More sharing options...
GingerRobot Posted January 8, 2009 Share Posted January 8, 2009 If you want the oldest first, krsort() instead. T'other way round. The regular sorting functions sort lowest to highest; the (k)rsort functions sort highest to lowest. Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732504 Share on other sites More sharing options...
premiso Posted January 8, 2009 Share Posted January 8, 2009 If you want the oldest first, krsort() instead. T'other way round. The regular sorting functions sort lowest to highest; the (k)rsort functions sort highest to lowest. Yea, sorry. My original line of thinking was file size lol. Thanks for correcting! Quote Link to comment https://forums.phpfreaks.com/topic/140003-sorting-my-directory/#findComment-732516 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.