Listing images within a directory

Asked

Viewed 871 times

2

I want to list the images from a directory, but I’m not getting it. You’re listing the right amount and everything, but the images don’t 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();
?>

2 answers

2

Best use directory Iterator.

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

foreach (new DirectoryIterator($path) as $fileInfo) {
    if ($fileInfo->isDot()) continue;
    echo $fileInfo->getFilename() . "<br />\n";
}
  • It worked but listed the names. and to list the images ?

  • so listed but does not pull the images I think it is by the .htacces.. that is asking direct link .. type http://www.meusite.com.br understand? my code now looks like this: <?php $directory = "manage/foto_portifolio/gallery/"; foreach (new Directoryiterator($directory) as $fileInfo) { if ($fileInfo->isDot()) continue; echo " <a class='fancybox' data-fancybox-group='gallery' href='". $directory." /". $fileInfo->getFilename()." ' ' title='> <img src='". $directory." /". $fileInfo->getFilename()." ' width='80' height='80'> </a> "; } ?>

  • So, you are putting as src the directory, at this point you have to change to your URL $path is referring to the physical path of the file (on the server) src is referring to the URL of the file ("virtual" path). You can do $url_base = "http://seudominio.com.br" . $path; E in the image use <img src="{$url_base}{$fileInfo->getFilename()}" />

  • <?php $url_base = "http://imaginew.com.br"; $directory = "manage/foto_portifolio/gallery/"; foreach (new Directoryiterator($directory) as $fileInfo) { if ($fileInfo->isDot()) continue; echo "<img src='. {$url_base}{$fileInfo->getFilename()}. ' height='80' >"; } /* ?>

  • I put so but is not listing the images is only getting the main .. I call more above...

    • 1 I think the solution is this same, just reference the paths correctly. I did some tests here and am posting below a reply with the result I got (following the tip of this reply).
  • Missing http or https at first. ($url_base = "http://www.bbbbb") (Stack overflow is removing http from the beginning of the base url, mount the full http URL: //www) Anyway, are you listing only 1? How many images are there in the folder?

Show 2 more comments

2

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

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

  • isDir: checks if it is a directory (changed from isDotWillian’s answer, because I thought it made more sense)

  • getFilename: Returns the file name of the current element of the directory

See a complete list here.

Continuing, first of all you need to use an output to receive script errors, then use error_reporting and ini_set right at the beginning of the script, after <?php:

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

Thus, you will receive a description of the errors, and it will be easier to debug. The error will be 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/gallery.php on line 7

In this case, trying to implement here the solution posted by @Willian, I realized that the problem was to indicate the paths, and that is to understand the difference between the request http made within the foreach and the indication of the path to the DirectoryIterator, made through the variable $path.

So if $path for:

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

You will receive the following error:

Fatal error: Uncaught Exception 'Unexpectedvalueexception' with message 'Directoryiterator::__Construct(http://www.imaginew.com.br/administrar/foto_portifolio/galeria/): failed to open dir: not implemented' in gallery.php on line 8

And this is because the DirectoryIterator search for the physical path of the file (which in linux is /var/www/...), as long as the src search by URL (virtual path).

Inversely, if you put in $path the way /var/www/html/etc... the DirectorIterator will find correctly, but you will not be able to use $path in the image path, as indicating the path so for example:

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

There will always be something like http://www.imaginew.com.br/var/www/html/administrar/foto_portifolio/galeria/ (what’s wrong with the request http would need to be http://www.imaginew.com.br/administrar/foto_portifolio/galeria/ - without the **/var/www/html**... but it will be clear if you include the error outputs as I suggested above). That’s because, if you do not indicate the full path the request http made by src already part of the path of the directory in which you are, 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 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 when calling the image:

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

So the standard 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()}' />";

}
  • 1

    Very good your answer, upvote

Browser other questions tagged

You are not signed in. Login or sign up in order to post.