OrangeTux Posted August 3, 2010 Share Posted August 3, 2010 Hello, I'm making a imagegallery and when someone upload a new image that image has to appear as first picture of the gallery. I'm using an array for the files, but how can I sort the files on date, newest first.? <?php $n = 0; $pathToThumbs = "images/tekeningen/"; $dir = opendir( $pathToThumbs ); while (false !== ($fname = readdir($dir))){ // strip the . and .. entries out if ($fname != '.' && $fname != '..'){ $tekeningen[$n] = $fname; } echo $tekeningen[$n]."<br />"; $n = $n +1; } closedir( $dir ); ?> I found solutions with ksort and filemtime, but I don't understand them and/or they didn't work for me. Link to comment https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/ Share on other sites More sharing options...
kenrbnsn Posted August 3, 2010 Share Posted August 3, 2010 To be able to sort the filenames by date you have to store the names and dates in an array, sort the array and then display the information on the screen, for example: <?php $pathToThumbs = "images/tekeningen/"; $f = glob($pathToThumbs . '/*'); $tmp = array(); foreach ($f as $file) { $tmp[$file] = filemtime($file); } arsort($tmp); foreach ($tmp as $file => $time) { echo basename($file) . "<br>\n"; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/#findComment-1094566 Share on other sites More sharing options...
OrangeTux Posted August 3, 2010 Author Share Posted August 3, 2010 It works half. I'm on a Linux system, btw, I don't know if that is a part of the problem. When I copy existing file's to the folder, the files are sorted random. They aren't sorted on physical place. But when I create new files in that folder these file's are sorted correct. What can be the problem? Link to comment https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/#findComment-1094770 Share on other sites More sharing options...
kenrbnsn Posted August 3, 2010 Share Posted August 3, 2010 Please post the code you're using. I tested my code on a Linux system. Ken Link to comment https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/#findComment-1094793 Share on other sites More sharing options...
OrangeTux Posted August 4, 2010 Author Share Posted August 4, 2010 I'm using the script that you've posted above. -+But I know already the problem.- When I copy an existing file to another folder the modification date won't change. e.g. x.txt is last modificated at 14:45 04-08-2010. I copy x.txt to another folder at 17:00 04-08-2010. x.txt in the new folder is still last modificited at 14:45 04-08-2010, not at 17:00 04-08-2010. Is there a way I can fix this or can do a trick to solve the problem? Link to comment https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/#findComment-1095014 Share on other sites More sharing options...
kenrbnsn Posted August 4, 2010 Share Posted August 4, 2010 Use the Linux touch command to change the last modified date of the file after you copy it. Ken Link to comment https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/#findComment-1095101 Share on other sites More sharing options...
OrangeTux Posted August 4, 2010 Author Share Posted August 4, 2010 Oke, thanks for helping me. Link to comment https://forums.phpfreaks.com/topic/209659-how-to-sort-array-with-files-on-date/#findComment-1095237 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.