views:

39

answers:

3

I have the following code

    // Define the full path to your folder from root 
    $path = "../galleries/".$album; 

    // Open the folder 
    $dir_handle = @opendir($path) or die("Unable to open $path"); 

    // Loop through the files 
    while ($file = readdir($dir_handle)) { 

              if(strlen($file)>1){echo "<a href='http://minification.com/?page_id=32&amp;dir=$album&amp;img=$file'&gt;&lt;img src='http://minification.com/galleries/$album/$file'&gt;&lt;/a&gt;";}

    } 

    // Close 
    closedir($dir_handle); 

What i want to do is pull in all the images from a folder and display them using PHP. So far its working up to the point where it only displays one image out of the folder. Anyone know how to fix this?

+3  A: 

Your second file probably evaulates to false, see readdir(), you should do:

while (false !== ($file = readdir($dir_handle))) {
Vinko Vrsalovic
+1 for beating me to it :P
Matt Williamson
Missing a paren though
Matt Williamson
ChrisMJ
Maybe it's permissions? Your user can see the 6 images but the webserver's user can only see one?
Vinko Vrsalovic
yep, basically if i navigate to the directory all the images are there, but its th script only is showing 1 image
ChrisMJ
@ChrisMJ: There is a thing called file permissions. Where you can grant or deny the right to read (or write) files. I'm suggesting that the pending 7 files are with a different set of permissions than the 1 file that shows on the script. Or a different owner.
Vinko Vrsalovic
@ChrisMJ: It could also be a path problem is possible, as Matt says, you could have another galleries/album folder with a single file. Anyway, it's no longer a programming problem if you use scandir or the fixed while(), and it's got to do with either your logic or your configuration or invalid parameters.
Vinko Vrsalovic
+1  A: 

try this:

while(false !== ($file = readdir($handle))) {

A lot of different values evaluate to false in php so you may be getting a false positive.

Matt Williamson
ChrisMJ
Are you checking that the image that is found has the name of an image in that folder? Perhaps it is finding the wrong folder due to relative path.
Matt Williamson
I would guess that the image it DID find was ".." since you are only checking if the len > 1
Matt Williamson
+2  A: 

Hint: If this is PHP 5, you can reduce the hassle a bit by using scandir instead.

Scytale
even using this function its still only finding the first image
ChrisMJ
here is the output of what i get Array ( [0] => . [1] => .. [2] => Screen shot 2010-08-09 at 15.21.12.png )there should be 8 or images
ChrisMJ