2015-03-13 110 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); 
相關問題