Jump to content

Sorting an opendir listing?


Eridian

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.