Forum Moderators: coopster

Message Too Old, No Replies

Cycling through all images in a folder

For photo gallery

         

KrazyKid

11:36 pm on Jun 20, 2006 (gmt 0)

10+ Year Member



Hello, I'm trying to create a Photo Gallery program with PHP, and I need to know how to create a loop that cycles through every image in a certain folder. The problem is that they can't just be named img1.jpg, img2.jpg, img3.jpg, etc. They will have different file extensions, and completely different names. Thanks :)

eelixduppy

12:16 am on Jun 21, 2006 (gmt 0)



You can try this. I'm not sure what you want to do once you loop through the image files. Here you go:

<?php
$dir = "page/to/images";
$good_ext = [url=http://us3.php.net/manual/en/function.array.php]array[/url](".jpg",".gif");

if ($handle = [url=http://us3.php.net/manual/en/function.opendir.php]opendir[/url]($dir)) {
while (false!== ($file = [url=http://us3.php.net/manual/en/function.readdir.php]readdir[/url]($handle))) {
$loc = [url=http://us2.php.net/manual/en/function.strrchr.php]strrchr[/url]($file,".");
$ext = [url=http://us2.php.net/substr]substr[/url]($file,$loc);
if([url=http://us3.php.net/in_array]in_array[/url]($ext,$good_ext)
{
//do something with file
echo "Good image file: ".$file."<br>";
}
else
{
echo "BAD IMAGE...BAD: ".$file."<br>";
}
}
closedir($handle);
}
else
{
echo "Directory does not exist!";
}
?>

**Note, this is the long way of checking for file extensions. You can just as easily use other methods, but you wouldn't learn as much would you ;) Here's a thread on other methods of doing so [webmasterworld.com]. Good luck (btw, this code was not tested)

Justus

4:18 am on Jun 22, 2006 (gmt 0)

10+ Year Member



It's telling me that everything is bad:

BAD IMAGE...BAD: .
BAD IMAGE...BAD: ..
BAD IMAGE...BAD: index.php
BAD IMAGE...BAD: jus jumping.jpg
BAD IMAGE...BAD: jus leaning back.jpg
BAD IMAGE...BAD: jus pizza.jpg
BAD IMAGE...BAD: jus sears.jpg
BAD IMAGE...BAD: jus sepia.jpg
BAD IMAGE...BAD: jus_poster-edges.jpg
BAD IMAGE...BAD: error_log

Also, I don't really understand how this works:

if ($handle = opendir($dir)) {
while (false!== ($file = readdir($handle))) {
$loc = strrchr($file,".");
$ext = substr($file,$loc);
if(in_array($ext,$good_ext))

I mean, I understand that it gets the filename, seperates the extension from it, and checks to see if it's jpg or gif. But how does it actually *work*? Would you mind explaining? Thanks a lot :)
Btw, I'm the same person as above.

eelixduppy

11:20 am on Jun 22, 2006 (gmt 0)



WOW....sorry about that, I should really look up the return values every once in awhile. This is the correct code:

$dir = "Lana_Naoum/images";
$good_ext = array(".jpg",".gif");

if ($handle = opendir($dir)) {
while (false!== ($file = readdir($handle))) {
$ext = strrchr($file,".");
echo $ext."<br>";
if(in_array($ext,$good_ext))
{
//do something with file
echo "Good image file: ".$file."<br>";
}
else
{
echo "BAD IMAGE...BAD: ".$file."<br>";
}
}
closedir($handle);
}
else
{
echo "Directory does not exist!";
}

Basically the directory is opened and $handle is the resource for that directory. Then while $file does NOT equal (!==) false (readdir Returns the filename of the next file from the directory.) the script uses some string functions to find the extension. So if the extension is after the last period, then we can use strrchr to find the last occurrence of a character(the period) in a string(the file name) each time we find a file. Then we check to see if the extension is in the array of good extensions, if it is we do something, if not something else. Hope this explains it a little better.

Sorry for that error, I'll make sure my script is working next time ;)
Good luck!

Nutter

1:00 pm on Jun 22, 2006 (gmt 0)

10+ Year Member



One thing to check that I messed up on doing something similar is to either strtolower or strtoupper before you do the comparison. I spent about half an hour going over a function that worked flawlessly on my machines and didn't work for a few zip files on a client's machine. Turns out I was checking to make sure the extension was .jpg and the extension on her files was .JPG. Of course that only matters on a Linux server.

Justus

1:33 am on Jun 23, 2006 (gmt 0)

10+ Year Member



Thanks a lot for all the help, eelixduppy, and thanks for the tip, Nutter :)
That should do it :D