Displaying images in order of their name?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
memzenator
Forum Newbie
Posts: 3
Joined: Fri May 14, 2010 5:13 pm

Displaying images in order of their name?

Post by memzenator »

Hello. :) I am using this code to display the images in a directory ("portraits")

Code: Select all

<?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%'>&nbsp;";
}
}
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.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Displaying images in order of their name?

Post by mecha_godzilla »

The simplest way would be to load all the filenames into an array and then sort them. Try this example:

Code: Select all

<?php
    $path = "./portraits";
    $image_array = array();
    $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)) {
            $image_array[] = $file;
        }
        
    }
    
    closedir($dir_handle);
    
    sort($image_array);
    
    foreach ($image_array as $key => $value) {
        echo "<img src='portraits/$value' height='100%'>&nbsp;";
    }
?>
Although the code isn't overly complex, things like arrays can catch you out. If you're new to PHP I would suggest taking a look at the main PHP site to see how the functions work, in this case for sort():

http://uk2.php.net/manual/en/function.sort.php

and then heading over to w3schools for some quick examples of how arrays work:

http://www.w3schools.com/php/php_arrays.asp

Also, if you're testing for file extensions it's a good idea to lowercase the whole filename for the purposes of your test. As an example, your script will test for image.jpeg but not IMAGE.JPEG as you would probably want it to do. You should also validate the filenames in case someone has created a filename with funny characters (either accidentally or for malicious reasons.) Another vulnerability is where people do something 'cute' like putting JavaScript code in a filename (an example of cross-site scripting) which is particularly dangerous if you're letting people upload their own files. The moral of that story - getting your script working is only half the battle, you then need to think about whether it's secure or not...once you start getting a conscience about doing stuff securely there's no way back - you have been warned! ;)

HTH,

Mecha Godzilla
memzenator
Forum Newbie
Posts: 3
Joined: Fri May 14, 2010 5:13 pm

Re: Displaying images in order of their name?

Post by memzenator »

Thank you so much, for the detailed answer! That worked! :D
The reason I had the capitalized JPG in there was because my photographer friend will be uploading the pictures, and for some reason most of them are capitalized.
memzenator
Forum Newbie
Posts: 3
Joined: Fri May 14, 2010 5:13 pm

Re: Displaying images in order of their name?

Post by memzenator »

By the way, the site is here, if you care: http://zackhenningsgaard.com
I just feel like you should see it since you helped me. :P
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: Displaying images in order of their name?

Post by mecha_godzilla »

Glad that worked for you...usually I get something like "hey, you lousy bum that script didn't work for me and I hate you" just because I missed out a semi-colon... :banghead:

I'll have a proper look at that site when I get a moment but there's certainly some nice pictures on there. If you need any more help just say so.

M_G
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: Displaying images in order of their name?

Post by Eran »

If you want to get files into an array, glob() provides a simpler way to do so:

Code: Select all

$path = "./portraits";
$images = glob($path . '/*.{jpg,gif,png,bmp}',GLOB_BRACE);
Post Reply