murratw Posted March 8, 2012 Share Posted March 8, 2012 I am trying to show images in a directory by date last modified. The code below is showing the oldest ones first then by file type. How do I modify it to show all files by last uploaded to the directory? I want the newest ones showing first. I am very new to PHP. <?php //Your folder $files = glob("images/*.*"); $colCnt=0; echo '<table border="0" style="width:1000px;">'; for ($i=1; $i<count($files); $i++) { $colCnt++; if ($colCnt==1) echo '<tr>'; echo '<td width="20%" style="font-size:8.5px; font-family:arial">'; $num = $files[$i]; echo ' <a href="' . $num . '"><img class="thumb ImgBorder" src="'.$num.'" rel="lightbox" alt="random image"></a>'." "; echo '</td>'; if ($colCnt==6) { echo '</tr>'; $colCnt=0; } } echo '</table>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/258531-simple-image-sorting/ Share on other sites More sharing options...
requinix Posted March 8, 2012 Share Posted March 8, 2012 The code below is showing the oldest ones first then by file type. It's actually showing them in whatever order the operating system and file system says. That tends to be oldest to newest but isn't guaranteed. glob() will get you an array of files. You need to sort this array based on whatever criteria you want, and usort is appropriate for that. $files = glob("images/*.*"); function sortnewestfilesfirst($a, $b) { return filemtime(???) - filemtime(???); } usort($files, "sortnewestfilesfirst"); If I tell you that: - usort() takes an array and a function to use to sort the array - the function takes two arguments and must return: a number 0 for the reverse, and =0 if they're the same - filemtime() returns the last modified time of a file as a Unix timestamp (ie, a number) with smaller numbers being older can you replace the ???s with the appropriate variables? Quote Link to comment https://forums.phpfreaks.com/topic/258531-simple-image-sorting/#findComment-1325317 Share on other sites More sharing options...
murratw Posted March 8, 2012 Author Share Posted March 8, 2012 I can't say I do know what to put for the ???'s. I am assuming it some kind of time variable. Quote Link to comment https://forums.phpfreaks.com/topic/258531-simple-image-sorting/#findComment-1325326 Share on other sites More sharing options...
requinix Posted March 8, 2012 Share Posted March 8, 2012 One of them is $a and one of them is $b. Quote Link to comment https://forums.phpfreaks.com/topic/258531-simple-image-sorting/#findComment-1325341 Share on other sites More sharing options...
murratw Posted March 8, 2012 Author Share Posted March 8, 2012 Wow, that worked very well. Thank you so much. It is doing something else interesting now. It sorts them correctly but leaves out the latest image. It starts with the second latest image. Any thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/258531-simple-image-sorting/#findComment-1325374 Share on other sites More sharing options...
DavidAM Posted March 8, 2012 Share Posted March 8, 2012 That's what you told it to do. for ($i=1; $i<count($files); $i++) Numeric arrays start at zero, unless you intentionally start it somewhere else. Quote Link to comment https://forums.phpfreaks.com/topic/258531-simple-image-sorting/#findComment-1325377 Share on other sites More sharing options...
murratw Posted March 8, 2012 Author Share Posted March 8, 2012 Thanks I figured that 1 was the culprit. Thanks for your help. works great now. Quote Link to comment https://forums.phpfreaks.com/topic/258531-simple-image-sorting/#findComment-1325382 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.