Listing images within a directory

2

I want to list the images in a directory, but I can not. You are listing the right amount and everything, but the images do not appear.

<?php
    $path = "http://www.imaginew.com.br/administrar/foto_portifolio/galeria/";
    $diretorio = dir($path);

    echo "Galeria de Imagens '<strong>".$path."</strong>':<br />";

    while($arquivo = $diretorio -> read()){ 
        echo " <img src='".$path."/".$arquivo."' width='80' height='80' class='img-thumbnail'>";
    }

    $diretorio -> close();
?>
    
asked by anonymous 18.11.2015 / 17:44

2 answers

2

Better to use directory Iterator.

$path = "/var/www/html/imagens";

foreach (new DirectoryIterator($path) as $fileInfo) {
    if ($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br />\n";
}
    
18.11.2015 / 17:51
2

As @Willian said in your answer, you can use the DirectoryIterator function to view the contents of directories and files with PHP.

This function will return an object with several data, which can be collected through indexes. The main ones you will use are:

  • isDir : Returns the filename of the current directory element

See a full here link.

Continuing, first of all you need to use an output to receive the script errors, then use isDot and getFilename at the beginning of the script, after error_reporting :

error_reporting(E_ALL);
ini_set("display_errors", 1);

This will give you a description of the errors, and it will be easier to debug. The error will look something like this:

  

Fatal error: Uncaught exception 'UnexpectedValueException' with   message   'DirectoryIterator :: __ construct (/ var / www / html / gallery /):   failed to open dir: No such file or directory 'in   /var/www/html/galeria.php on line 7

In this case, trying to implement here the solution posted by @Willian, I realized that the problem was precisely to indicate the paths, and this means understanding the difference between the ini_set request made within <?php and the path indication for http , made through the foreach variable.

So, if DirectoryIterator is:

$path = "http://www.imaginew.com.br/administrar/foto_portifolio/galeria/";

You will get the following error:

  

Fatal error: Uncaught exception 'UnexpectedValueException' with   message   'DirectoryIterator :: __ construct ( link ):   failed to open dir: not implemented 'in galeria.php on line 8

And this occurs because $path looks for the physical path of the file (which in linux is $path ), while DirectoryIterator looks for the URL (virtual path).

Conversely, if you put /var/www/... the path src the $path will find correctly, but you can not use /var/www/html/etc... in the path of the image, for indicating the path like this:

echo " <img src='".$path."/".$fileInfo->getFilename()."' width='80' height='80' class='img-thumbnail'>";

It will always be something like DirectorIterator (which is wrong, in $path request would have to be http://www.imaginew.com.br/var/www/html/administrar/foto_portifolio/galeria/ - without http ... but this will be clear if you include the error outputs as I suggested above). This is because if you do not indicate the full path the http://www.imaginew.com.br/administrar/foto_portifolio/galeria/ request is already part of the directory path you are in, and takes the value of the variable as the remainder of the path.

The solution is to create another variable to be used when searching for the image:

$path = "/var/www/html/administrar/foto_portifolio/galeria/"; // esta é pra usar no 'DirectorIterator'

$dirPath = "http://www.imaginew.com.br/administrar/foto_portifolio/galeria/"; // e esta é pra usar dentro do 'foreach' ao chamar a imagem

Or just indicate the full path at the time of calling the image:

echo " <img src='http://www.imaginew.com.br/administrar/foto_portifolio/galeria/{$fileInfo->getFilename()}' />";

Then the normalized code looks like this:

$path = "/var/www/site/html/administrar/foto_portifolio/galeria/";
$dirPath = "http://www.imaginew.com.br/administrar/foto_portifolio/galeria/";

foreach (new DirectoryIterator($path) as $fileInfo) {
    if ($fileInfo->isDir()) continue;
    echo $fileInfo->getFilename() . "<br />\n";
    echo " <img src='$dirPath{$fileInfo->getFilename()}' />";

}
    
18.11.2015 / 20:31