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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.