June 20, 200818 yr 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!
July 2, 200818 yr 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.
July 2, 200818 yr Ok, here you go: http://www.php.net/manual/en/function.readdir.php#75132 This dude created his own script that you have to download (preg_find.php) here: http://www.pgregg.com/forums/viewtopic.php?tid=73
July 2, 200818 yr 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
July 2, 200818 yr I swear I had already helped scaz182 with the EXACT same issue last week lol http://www.webdesignerforum.co.uk/index.php?showtopic=7974
July 3, 200818 yr I swear I had already helped scaz182 with the EXACT same issue last week lol http://www.webdesignerforum.co.uk/index.php?showtopic=7974 sorry shaun ... missed that one
Create an account or sign in to comment