Eridian Posted April 6, 2010 Share Posted April 6, 2010 PHP novice here! I have the following code that is working properly: <?php $dir = opendir ("puppyweek5"); while (false !== ($file = readdir($dir))) { if (strpos($file, '.gif',1)||strpos($file, '.jpg',1) ) { $week_array[] = ($file); } } if ($week_array) { sort ($week_array); foreach ($week_array as $file) { echo "<img src=\"puppyweek5/$file\" border=\"1\"><br /><br />"; } } ?> I have various directories where I have photos stored. All my photos begin with the date (yy.mm.dd) and a hyphen (for instance, "100406-"). I take anywhere from 0 to a few hundred photos a day, then upload the ones I want to be displayed using that code. Here is a sampling of yesterday's files: 100405-13.jpg 100405-21.jpg 100405-22.jpg 100405-36.jpg 100405-118.jpg 100405-156.jpg 100405-178.jpg 100405-234.jpg This is also the order (alphanumerical) I want them to be displayed in. However, using the code I mentioned above, they are currently being displayed as: 100405-13.jpg 100405-118.jpg 100405-156.jpg 100405-178.jpg 100405-21.jpg 100405-22.jpg 100405-234.jpg 100405-36.jpg How can I fix this without [physically] changing any filenames? (I have thousands of photo files from many years and have no interest in changing any of their names.) TiA! Link to comment https://forums.phpfreaks.com/topic/197725-sorting-an-opendir-listing/ Share on other sites More sharing options...
anupamsaha Posted April 6, 2010 Share Posted April 6, 2010 Hi, It seems you cannot do anything much with the "sort" command. The output of the command is correct as you can see its a string sorting. Thanks. Link to comment https://forums.phpfreaks.com/topic/197725-sorting-an-opendir-listing/#findComment-1037652 Share on other sites More sharing options...
ignace Posted April 6, 2010 Share Posted April 6, 2010 You need natsort which applies the "natural order sorting" algorithm. This algorithm addresses this problem. Link to comment https://forums.phpfreaks.com/topic/197725-sorting-an-opendir-listing/#findComment-1037671 Share on other sites More sharing options...
Eridian Posted April 6, 2010 Author Share Posted April 6, 2010 BRILLIANT. I just knew there had to be a simple way to go about this. Thank you very much! Link to comment https://forums.phpfreaks.com/topic/197725-sorting-an-opendir-listing/#findComment-1037905 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.