扫描文件夹中的文件夹,并获取文件夹的第一张图像


Scan folder for folders, and get first image of folder

我在一个文件夹中有多个文件夹。我正在尝试制作一种画廊。

我想扫描第一个文件夹(文件夹 A)以查找其中的所有文件夹。

接下来我要做的是获取该文件夹的第一张照片,忽略所有不是图像的内容。

它必须是每个文件夹中第一个图像的预览。

RecursiveDirectoryIterator 可以帮助您迭代目录树。

$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);

我做了一些额外的研究,以下内容对我有用:

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?
                                       }
                                     }
                            }
包含

包含图像的文件夹的文件夹是"实现"。对于GLOB,我首先浏览了它们。之后,我将所有文件夹名称放在一个数组中。

有了这个数组,我又做了一个循环。我再次使用glob查看该文件夹中的内容。之后,我对图像进行了排序,并将预览图像设置为最后添加的图像。