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 - order readdir() files alphabetically

Featured Replies

Hi people,

 

I tried to ask this question the other day but was mega tired so don't think I made sense resulting in no replies.

 

Basically what I want to know is; can you order readdir() files alphabetically?

 

When I was on a Windows server the files in the directory were automatically returned in alphabetical order but now I've moved onto Linux I am getting a seemingly random order!

 

Please help it is driving me crazy!

Have you tried putting the file names into an array and using the sort() function?

  • Author

Yeh, whilst researching ways to do this I also came across putting it into an array then printing that but the trouble I have is that I have two readdir() functions going on in the same loop one for the thumbnail and one for the image.

 

When I tried putting it into an array I could only print out one array at a time!

 

This is my code (hope you can understand what I'm getting at!)

 

// 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 from 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++;

}

 

here is a link to what I am producing - as you can see because the thumbnails and the main images are read using readdir() in a seemingly random order the thumnails do not correspond to the main images!

 

Any more insight/help would be much appreciated

Here is a quick function I cooked up for the thumbnails, duplicate it and change necessary variables and of course the function name to do the same for the images.

 

function sort_thumbs(){

// Create array
$matches = array();

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

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

// Loop through files and add them to array
while (($file = readdir($thumbnails)) == TRUE) {
if($file != "." || $file != ".."){
	$matches[] = $file;
}

}

// Return the array
return $matches;

}

// Define new array and sort alphabetically
$thumbs = sort_thumbs();
sort($thumbs);

 

 

Then just loop through the new array using FOR. I must warn you, this code was not tested!

  • Author

Thanks for the involved help Shaun, I have tried that (or similar) yesterday and the issue I had with it was that I need to echo out both the thumbnail name and the main image name within the same loop (I cannot work out how two separate arrays can be printed out within the same loop?)

 

I need them to both be echoed out at the same time because the html code contains both the thumbnail and image name in the same line being the $file and $tfile variables:

 

i.e.

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";

 

I might have the wrong end of your code though?

  • Author

ff

 

EDIT: god knows how I accidentally posted this

Are the thumnail filenames the same as the full image filenames?

Ok, I see the only difference is there is a 't' in the thumb version... try this after my last code:

 

for($i=0;$i<count($thumbs);$i++){

$tn = $thumb[$i];
$file = str_replace("t", "", $tn);

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

}

 

You should not need to loop through the images directories. This should work. You may want to obviously throw in some precautions in there (i.e. if(file_exists()) etc)

 

EDIT: assuming the thumbs dont have any other t's in their names lol! if so, you can use functions like strstr for example to remove the unwanted 't'.)

  • Author

Ahh no there not, but now you mention it there is no real reason why they can't be the same, meaning I could use a foreach loop to print the array and use the variable given to the array elements twice (once for the thumbnail and once for the main image! but obviously with different folder paths.)

 

mmm I think you may well have solved my problem!

 

although this sorta feels like a coincidental solution and for some reason would still be interested in doing it using different file names for the thumbnails and the main images?

 

but if you or anybody cannot work out how then this will more than suffice!

lol, we must have posted that at the same time! Does my last post not help with this new query??

  • Author

Yeah lol, hate it when that happens!

 

ok, forgive me if I am not entirely grasping your code but does that also assume (which it easily could be) that the thumbs are named tNAME_01.jpg, tNAME_02.jpg, tNAME_03.jpg and the main images are named NAME_01.jpg, NAME_02.jpg, NAME_03.jpg?

 

Does the str_replace(); function essentially look for a 't' within the file name and then replace it with a space? what if the file contained a 't' elsewhere? e.g. NAME.tiff (this is not really an issue with my project but am just interested in the nature of str_replace();?)

 

Cheers

 

EDIT: oops I just read your edit which basically answers this!!

lol yeah I did mention that in my edit but we must have posted at the same time again!

 

It will take out any 't' in the filename and replace it with nothing (so it will essentially remove it). But if it is always in the same place in the filename (not necessarily the first character), you can use another function instead of str_replace to get the exact position of the specific 't' and remove it. Maybe something like strstr()?

 

I hope we are still on the same page lol.

  • Author

Ok well I'm gonna give this a go in the morning!

 

Thanks v.much for your help I'm pretty certain it will work/ I understand what you are saying!

 

I still have so much to learn with PHP! lol

And again lol, we might aswell be in live chat!

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.