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! Quote Link to comment 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. Quote Link to comment 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. Quote Link to comment 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! 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.