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 isDot
Willian’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()}' />";
}
It worked but listed the names. and to list the images ?
– João Batista Lucas Chaves
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> "; } ?>
– João Batista Lucas Chaves
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()}" />
– William Okano
<?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' >"; } /* ?>
– João Batista Lucas Chaves
I put so but is not listing the images is only getting the main .. I call more above...
– João Batista Lucas Chaves
– gustavox
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?
– William Okano