0

I just can't get this to work what is wrong here?

 $files = scandir('gallery/');
 if ($files !== false) 
 {
    foreach($files as $f) {
      if ($f == '..' || $f == '.')
          continue;     
      echo '<li><img src="gallery/'.$f.'" alt="'.$f.'" title=""></li>'."\n";
    }
 } 

the folder is in my root folder named "gallery". The images are inside..

I always get this error:

Warning: Invalid argument supplied for foreach() in "MYDOCUMENT.PHP" on line 101.

7
  • What’s the output of var_dump($files);? Commented Jan 30, 2014 at 13:30
  • can you be more specific, what do i have do to? I'm new to php... Commented Jan 30, 2014 at 13:32
  • 1
    On the line just below $files = scandir('gallery/'); put var_dump($files); die(); and run the code. Give us the output. Commented Jan 30, 2014 at 13:33
  • 2
    You might want to consider using glob() . $files = glob('gallery/*.{jpeg,gif,png}', GLOB_BRACE); Commented Jan 30, 2014 at 13:35
  • Check is_array($files) and the try foreach Commented Jan 30, 2014 at 13:36

4 Answers 4

2
<?php
$files = scandir('gallery/');
if ($files !== false) 
{
    foreach(is_array($files) as $f) {
    if ($f == '..' || $f == '.')
?>
    <li><img src="<?php echo 'gallery/'.$f ?>" alt="<?php echo $f ?>" title="">
<?php
   }
} 
?>
Sign up to request clarification or add additional context in comments.

1 Comment

How does it work? The function is_array will return true if $files is an array, false otherwise. And how do you iterate over a boolean value with a foreach loop?
2

This works, for me...:

<?php
    $files = scandir('gallery/');
    if ($files !== false) {
        foreach($files as $f) {
            if ($f == '..' || $f == '.') continue;
            echo $f . "<br>\n";
        }
    }
?>

It's quite the same code as yours... So check the "gallery/" directory contents...

5 Comments

ok this echos the names of the images. how can i echo the images itself?
O.k... Your code prints html which correctly references <src> image tags... So you should check the images are readable by the web server user...
hm they should be readable, why shouldn't they?Like i said the code prints the names of the images but doesn't display the images
The logic is: PHP code prints HTML with the name of the images. The browser (IE, Firefox, Chrome, ...) which interpets the HTML retrieves the images from the server and displays them...
i was missing a '/' before the variable.. my bad. it works now thank you very much
1

change foreach to:

 foreach((array)$files as $f)

the code may be:

 $files = scandir('gallery/');
 if ($files !== false) 
 {
    foreach((array)$files as $f) {
      if ($f == '..' || $f == '.')
          continue;     
      echo '<li><img src="gallery/'.$f.'" alt="'.$f.'" title=""></li>'."\n";
    }
 } 

1 Comment

didn't change anything
1

You shouldn't validate the array with :

if ($files !== false) 

But by doing :

if (is_array($files) && count($files) > 0) {

Or else you'll get warnings like this one.

[EDIT] And consider using glob like one guy said. This will work with that function.

Example:

if (is_array($files) && count($files) > 0) {
   foreach(glob('gallery/*.{jpeg,gif,png}', GLOB_BRACE)) {
      // code here
   }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.