memzenator Posted May 14, 2010 Share Posted May 14, 2010 Hello. I am using this code to display the images in a directory ("portraits") <?php $path = "./portraits"; $dir_handle = @opendir($path) or die("Unable to open folder"); while (false !== ($file = readdir($dir_handle))) { if(ereg("(.*)\.(jpg|JPG|bmp|jpeg|png|gif)", $file)){ echo "<img src='portraits/$file' height='100%'> "; } } closedir($dir_handle); ?> and I'd like to have them displayed in order of their name. They are named "01", "02", and so on. I think this should be pretty simple.. I don't know much about php, so any help would be great. Thanks. Link to comment https://forums.phpfreaks.com/topic/201810-displaying-images-in-order-of-their-name/ Share on other sites More sharing options...
.Stealth Posted May 14, 2010 Share Posted May 14, 2010 You could stick them in an array and use sort() http://php.net/manual/en/function.sort.php Link to comment https://forums.phpfreaks.com/topic/201810-displaying-images-in-order-of-their-name/#findComment-1058562 Share on other sites More sharing options...
litebearer Posted May 15, 2010 Share Posted May 15, 2010 might try here http://www.nstoia.com/toh/listdir.php # The script reads through a given folder # and displays the files contained therein # # There are five variables you may alter # # 1. $allowed_ext - this array is used to set which # type of file will be listed # # 2. $directory - this is used to set which directory # will be searched/listed # # 3. $do_link - TRUE means display the file # name as a link to the file; FALSE means # do NOT make the name a link # # 4. $sort_what - this determines what the # data will be sorted upon: 0 is by name; # 1 is by size; 2 is by date last modified # # 5. $sort_how - describes how to sort; # 0 means to sort ASCENDING # (oldest to newest, smallest to largest, Z to A); # while 1 means to sort DESCENDING Link to comment https://forums.phpfreaks.com/topic/201810-displaying-images-in-order-of-their-name/#findComment-1058575 Share on other sites More sharing options...
kenrbnsn Posted May 15, 2010 Share Posted May 15, 2010 Using the glob function makes this so much easier: <?php $files = glob($path . '/*.{jpg|JPG|bmp|jpeg|png|gif}',GLOB_BRACE); foreach ($files as $file) { echo "<img src='$file' height='100%'> "; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/201810-displaying-images-in-order-of-their-name/#findComment-1058596 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.