-2

I've got multiple folders within a folder. I'm trying to make a type of gallery.

I want to scan the first folder (FolderA) for all the folders within it.

Next thing I want to do is get the first picture of that folder, ignoring everything that is not a image.

It need's to be a preview of the first image in each folder.

ToluT
  • 21
  • 7
  • Did you already try to write some code? – testo Mar 13 '15 at 09:03
  • possible duplicate of [PHP read sub-directories and loop through files how to?](http://stackoverflow.com/questions/2014474/php-read-sub-directories-and-loop-through-files-how-to) – Pete Mar 13 '15 at 09:05
  • Thank's for the help but already figured it out. – ToluT Mar 16 '15 at 15:04

2 Answers2

0

RecursiveDirectoryIterator can help for you to iterate a directory tree.

$path = '/path/to/FolderA';

$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator($path));
$firsts   = array();
foreach($iterator as $name => $item){
    if (!$item->isDir()) {
        if (!isset($firsts[$item->getPath()]) && exif_imagetype($item->getRealPath())) {
            $firsts[$item->getPath()] = $item->getRealPath();
        }
    }
}

var_dump($firsts);
dzsubek
  • 91
  • 3
0

I've done some extra research and the following worked for me:

foreach(glob('cms/images/realisaties/*', GLOB_ONLYDIR) as $dir) {
                                    $dirname = basename($dir);

                                    $mappen[] = $dirname;
                                }

                                    foreach($mappen as $map){

                                    $search_dir = "cms/images/realisaties/".$map;
                                       $images = glob("$search_dir/*");
                                       sort($images);


                                       if (count($images) > 0) { 
                                           $img = $images[0]; 




                                       echo '

                                                <!--product item-->
                                                <div class="product_item hit w_xs_full">
                                                    <figure class="r_corners photoframe type_2 t_align_c tr_all_hover shadow relative">
                                                        <!--product preview-->
                                                        <a href="realisaties/40-realisaties/'.$map.'" class="d_block relative wrapper pp_wrap m_bottom_15" >
                                                            <img src="'.$img.'" class="tr_all_hover" alt="0" style="max-height:242px">
                                                        </a>
                                                        <!--description and price of product-->
                                                        <figcaption>
                                                            <h5 class="m_bottom_10"><a href="realisaties/40-realisaties/'.$map.'" class="color_dark">'.ucfirst($map).'</a></h5>
                                                            <a href="realisaties/40-realisaties/'.$map.'"><button class="button_type_12 bg_scheme_color r_corners tr_all_hover color_light mw_0 m_bottom_15">Bekijk</button></a>
                                                        </figcaption>
                                                    </figure>
                                                </div>

                                       ';

                                         } else {
                                           // possibly display a placeholder image?
                                       }

                                     }
                            }

The folder containing the folders that had the images is "realisaties". With GLOB I first went through them. After that I put all the folder names in an array.

With that array I made another loop. I used glob again to look what is inside that folder. After that I sorted the images, and set the preview image to be the last added.

ToluT
  • 21
  • 7