Page 1 of 1
Displaying images in order of their name?
Posted: Fri May 14, 2010 5:18 pm
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%'> ";
}
}
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.
Re: Displaying images in order of their name?
Posted: Fri May 14, 2010 6:23 pm
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%'> ";
}
?>
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
Re: Displaying images in order of their name?
Posted: Fri May 14, 2010 6:33 pm
by memzenator
Thank you so much, for the detailed answer! That worked!

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.
Re: Displaying images in order of their name?
Posted: Fri May 14, 2010 6:41 pm
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.

Re: Displaying images in order of their name?
Posted: Fri May 14, 2010 7:11 pm
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...
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
Re: Displaying images in order of their name?
Posted: Fri May 14, 2010 7:29 pm
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);