June 24, 200818 yr 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!
June 24, 200818 yr Have you tried putting the file names into an array and using the sort() function?
June 24, 200818 yr 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
June 24, 200818 yr 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!
June 24, 200818 yr 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?
June 24, 200818 yr 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'.)
June 24, 200818 yr 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!
June 24, 200818 yr lol, we must have posted that at the same time! Does my last post not help with this new query??
June 24, 200818 yr 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!!
June 24, 200818 yr 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.
June 24, 200818 yr 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
Create an account or sign in to comment