0

I have some code in PHP and HTML, I'm trying to create a photo gallery, my function reads all the png/jpg/gif files in a folder then displays them on a table in my browser, this is my code.

<div class="container">
<div class="jumbotron">
    <table class="table table-hover">
        <?php
        $contador = 1;
        $directory="galeria";
        $dirint = dir($directory);
        while (($archivo = $dirint->read()) !== false){
            if (eregi("gif", $archivo) || eregi("jpg", $archivo) || eregi("png", $archivo)){
                if($contador == 5){
                    echo '</tr><tr>';
                    $contador = 1;
                }
                echo '<td valign="bottom"><img class="img-rounded" src="'.$directory."/".$archivo.'" width="250px"></td>';
                $contador++;
            }
        }
        $dirint->close();
        ?>
    </table>
</div>

When running this file PHP gives me this error: MyImage

I only have two images in my folder (the same image, copied), why am I getting so many errors as if it was looping through a lot of files?

Omaruchan
  • 403
  • 1
  • 5
  • 12
  • Your while loop loops through all files in a dir, only 2 meet the condition to be displayed. – AbraCadaver Oct 27 '15 at 18:55
  • You're calling `eregi` three times in every iteration of the loop. `dir()` will be getting all the files in that directory, plus the `.` and `..` directories, so your loop is being run a total of 12 times. – andrewsi Oct 27 '15 at 18:56
  • 1
    Try `glob($directory.'/*.{gif,jpg,png}')` – AbraCadaver Oct 27 '15 at 19:09
  • glob did the work perfectly! I had to read and tweak the code around a bit but it works now, thank you very much. – Omaruchan Oct 28 '15 at 00:22

0 Answers0