Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP readdir(); alphabetical order?

Featured Replies

Hi people,

 

I have made a simple photo gallery which reads all files within various directories and then spits out the thumbnails and the main image.

 

In order for the thumnails to correspond to the correct image then there must be some order to the readdir() function. Windows server managed to loop through them in alphabetical order but now I have moved back to Linux it gives me them in a seemingly random order.

 

Can anybody see why Linux would do this and if there is a method for sorting the readdir() function? I have been searching google and most places suggest putting the readdir() results into an array then sorting the array. This has proved difficult for me as I need two arrays - both the thumbnail and the main image that would need to be echoed at the same time?

 

here is my code

// Define the path to folder
$path = "./other/lightbox/images/rps";

$tpath = "./other/lightbox/images/trps";

// Open image folder
$images = @opendir($path) or die("Unable to open folder");

// Open thumbnail folder
$thumbnails = @opendir($tpath) or die("Unable to open folder");

//Print title
echo "<h1>Royal Photographic Society</h1>\n";

// create alt attribute number
$num = 1;
// Loop through the files
while (($file = readdir($images)) && ($tfile = readdir($thumbnails)) == TRUE) {

//Prevent folders showing
if($file == ".")
continue;
if($file == "..")
continue;
//Prevent thumb folders form showing
if($tfile == ".")
continue;
if($tfile == "..")
continue;


// Display the results
echo "<a href=\"/other/lightbox/images/rps/$file\" rel=\"lightbox[rps]\" title=\"RPS Photographs\"><img src=\"/other/lightbox/images/trps/$tfile\" width=\"85\" height=\"85\" alt=\"RPS_$num\" /></a>\n";

$num++;

}
echo "<br/>";
// Close it
closedir($images);
closedir($thumbnails);

 

If anybody has any idea what I am trying to say and knows of a solution then I would be one grateful guy!

  • 2 weeks later...

I'm wondering how to do the same thing.

 

Except I'm one step ahead of you. My script (using readdir()) seems to get the images in random order but I dynamically create thumbnail images. So it's not that much of an issue to me.

 

You should check out: http://shiftingpixel.com/2008/03/03/smart-image-resizer/

 

The dynamic thumbnail images are in fact smaller images and not just resized via html. So they load really fast. You can also control the quality of the resized image.

 

So it's like this: <a href="image.jpg"><img src="image.php?source-of-image&width=100&height=100"></a>

 

But it would still be nice to organize the images in a certain way.

hey guys ... instead of outputting the files

// Display the results
echo "<a href=\"/other/lightbox/images/rps/$file\" rel=\"lightbox[rps]\" title=\"RPS Photographs\"><img src=\"/other/lightbox/images/trps/$tfile\" width=\"85\" height=\"85\" alt=\"RPS_$num\" /></a>\n";

you can store them in an array (for example):

$my_array [$file] = $tfile;

then sort the array using the index (in this case $file)

asort($my_array);

and then create a loop for each array element to display the images

foreach($my_array as $key => $value)
{
 //display results
 echo $key;	// is $file
 echo $value;  //is $tfile
}

untested code provided ;)

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.