2015-03-13 65 views
-2

我有一个文件夹内的多个文件夹。我正在尝试制作一种类型的画廊。扫描文件夹中的文件夹,并获取文件夹的第一个图像

我想扫描其中所有文件夹的第一个文件夹(FolderA)。

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

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

+0

您是否已经尝试编写一些代码? – doev 2015-03-13 09:03:50

+0

[PHP读取子目录和循环文件如何?]的可能重复(http://stackoverflow.com/questions/2014474/php-read-sub-directories-and-loop-through-files-how-to ) – Pete 2015-03-13 09:05:46

+0

感谢您的帮助,但已经知道了。 – ToluT 2015-03-16 15:04:53

回答

0

我已经做了一些额外的研究,并为我下面的工作:

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

            } 
          } 

包含有图像文件夹中的文件夹是“realisaties”。有了GLOB,我首先通过了他们。之后,我把所有的文件夹名称放在一个数组中。

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

0

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); 
相关问题