zolder Posted May 4, 2009 Share Posted May 4, 2009 This code loading all images from folder 'test' but in name order - 'form A to Z'. I want to load images invertly - 'from Z to A'. Do You have any ideas? <?php if ($handle = opendir('test')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.html") { echo "<img src=test/$file></a>"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156775-solved-loading-images-in-back-order-from-z-to-a/ Share on other sites More sharing options...
jackpf Posted May 4, 2009 Share Posted May 4, 2009 You could use array_push() to compile an array of all of the file names, and then use array_reverse() to reverse it Quote Link to comment https://forums.phpfreaks.com/topic/156775-solved-loading-images-in-back-order-from-z-to-a/#findComment-825574 Share on other sites More sharing options...
Axeia Posted May 4, 2009 Share Posted May 4, 2009 Depending on what else is in the folder you could try looking at http://nl3.php.net/manual/en/function.scandir.php it comes with an optional "sorting order" parameter. Quote Link to comment https://forums.phpfreaks.com/topic/156775-solved-loading-images-in-back-order-from-z-to-a/#findComment-825579 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 Cleaner/easier to use glob $images = glob("test/*.jpg"); rsort($images); foreach($images as $image) { echo '<img src="'.$image.'" />'; } Quote Link to comment https://forums.phpfreaks.com/topic/156775-solved-loading-images-in-back-order-from-z-to-a/#findComment-825587 Share on other sites More sharing options...
zolder Posted May 4, 2009 Author Share Posted May 4, 2009 And if I want to use a thumbnails from other folder (test2), like this: <?php if ($handle = opendir('test')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != ".." && $file != "index.html") { echo "<a href=test/$file><img src=test2/$file></a>"; } } closedir($handle); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/156775-solved-loading-images-in-back-order-from-z-to-a/#findComment-825590 Share on other sites More sharing options...
wildteen88 Posted May 4, 2009 Share Posted May 4, 2009 Yes that'll work. However glob can still be used define('IMAGE_DIR', 'test'); define('THUMB_DIR', 'test2'); $images = glob(IMAGE_DIR."/*.{png,jpg,bmp}", GLOB_BRACE); rsort($images); foreach($images as $image) { $thumb = str_replace(IMAGE_DIR, THUMB_DIR, $image); echo '<img src="'.$image.'" /><br />'; echo '<img src="'.$thumb.'" />'; } Quote Link to comment https://forums.phpfreaks.com/topic/156775-solved-loading-images-in-back-order-from-z-to-a/#findComment-825592 Share on other sites More sharing options...
zolder Posted May 4, 2009 Author Share Posted May 4, 2009 Big thanks . Everything working fine. Thanks for help . Quote Link to comment https://forums.phpfreaks.com/topic/156775-solved-loading-images-in-back-order-from-z-to-a/#findComment-825600 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.