0
votes

I'm trying to make an image gallery that scans a main directory and creates a separate album for each subdirectory.

My structure is similar to this:

-Gallery --Subdir 1 ---Image 1 ---Image 2 --Subdir 2 ---Image 1 ---Image 2

The idea is that each album is going to be made of a div with a class of web-gallery. Then there will be a header for the album title made from the subdirectories name. After that a list is generated of each image. This is going to be a one page gallery. If possible I would like to have a variable that sets how many albums are listed that way if I have 30 subdirectories my page doesn't get too crowded.

So far I've written this but it doesn't work. I'm not getting any errors or logs though it just doesn't work.

$dirs = glob('img/gallery_temp/*', GLOB_ONLYDIR);
   foreach($dirs as $val) {
  echo '<div class="web-gallery">';
  echo "<h3><span>&raquo;</span> ".basename($val). "</h3>"; 
  echo '<ul class="web-gallery-list">';
 $files = glob($val.'*.{jpg,png,gif}', GLOB_BRACE);
 foreach($files as $file) {
 echo "<li><a href='".$file."'><img src='" . $file . "' alt='description'></a></li> \r\n";
 }
echo "</ul>";
echo "</div>";
}
3
"It doesn't work" isn't a very clear description of the problem. Can you be more specific? - Blackwood
It does nothing at all. Nothing is written from the echo. No errors and my error reporting is on. And nothing is being logged in errors either. - Jesse Elser
output the var_dump($dirs); or may be the path specified in glob is wrong - Tamil Selvan C
Path was wrong. But when I fixed it the result was each subdirectories name was being listed as a header element but no images have resulted - Jesse Elser
I think it is not returning files because you didn't add a / (slash) before *.{jpg,png,gif}. I think $val doesn't have that final slash (this is just a guess). - Zeke

3 Answers

1
votes

Simply add a / before *.{jpg,png,gif} like this:

$files = glob($val.'/*.{jpg,png,gif}', GLOB_BRACE);

This is because $val doesn't have a final / for the directory.

1
votes

You might consider using "readdir" instead of glob. Glob is to find pathnames matching a pattern, see here: http://php.net/manual/en/function.glob.php and is known to be a bit problematic.

Readdir, if your directory is entirely images might be easier to use: http://php.net/manual/en/function.readdir.php

Couple this with is_dir() http://php.net/manual/en/function.is-dir.php to resolve your directories vs files. Here is a snippet

<?php
    if ($handle = opendir('/galleries')) {
        while (false !== ($entry = readdir($handle))) {

            // this is a subdirectory
            if (is_dir($entry)) {

            }
            // this is a file
            else {
                echo $entry;
            }
        }
        closedir($handle);
    }
?>

If you make it a recursive function you could actually have it traverse a number of subdirectories creating galleries within galleries.

I also found this fantastic little snippet that is very elegant on another stack question: Get Images In Directory and Subdirectory With Glob

$rdi = new RecursiveDirectoryIterator("uploads/prevImgs/");
$it = new RecursiveIteratorIterator($rdi);
foreach($it as $oneThing)
    if (is_file($oneThing))
        echo '<img src="'.$oneThing.'" /><br />';
1
votes

Using SPL Library (PHP >= 5)

Better solution in your case is to use SPL library (the most cross-platform)

$directory = new RecursiveDirectoryIterator("./img/gallery_temp", FilesystemIterator::SKIP_DOTS);
// Flatten the recursive iterator, folders come before their files
$it  = new RecursiveIteratorIterator($directory, RecursiveIteratorIterator::SELF_FIRST);

foreach($it as $fileinfo)
{
    if($fileinfo->isDir())
    {
        // prevPath used to separate each directory listing and closing the bracket UL list
        $prevPath = $it->getSubPath().DIRECTORY_SEPARATOR.$fileinfo->getFilename();
        echo sprintf
        (
            "<div class='web-gallery'>
            <h3><span>></span> %s</h3>
            <ul>".PHP_EOL,
            $fileinfo->getFilename()
        );
    }

    if($fileinfo->isFile())
    {
        echo sprintf("<li><a href=''><img src='%s/%s' alt='description'></a></li>".PHP_EOL,  $it->getSubPath(), $fileinfo->getFilename());
        if($prevPath != $it->getSubPath())
            echo("</ul>");
    }
}

Note: For more informations : SPL Documentation

  • DIRECTORY_SEPARATOR is a cross-platform constant, will use the correct directory separator of the OS where are executed the code
  • FilesystemIterator::SKIP_DOTS, avoid to fetch the '.' and '..' dir link level.
  • you can limit the depth of scanning with $it->setMaxDepth(5);