2014-12-26 120 views
1

我試圖插入html文件夾「assets/imagens /」和任何子文件夾中的所有圖像。我還需要幫助來驗證不要回顯子文件夾。從文件夾和子文件夾插入圖像php

進出口使用下面的代碼:

    $path = "assets/imagens/"; 
        $diretorio = dir($path); 
        while($arquivo = $diretorio -> read()){ 
        if($arquivo != '.' && $arquivo != '..'){ 
         echo '<div href="#" class="list-group-item">'; 
         echo '<span class="close-button" secao="imagens">x</span>'; 
         echo "<img class='max-width' src='".base_url().$path.$arquivo."' />"; 
         echo '</div>'; 
        } 
        } 
        $diretorio -> close(); 
+0

好奇 - 究竟什麼是當前的問題,而你卻被經歷? – Ohgodwhy

+0

您需要檢查當前$ arquivo是否爲文件夾或文件,如果您需要遞歸執行該子文件夾的相同過程。 –

回答

0
     $path = "assets/imagens/"; 
         echo_directory_images($path); 

function echo_directory_images($path) 
{ 
        $diretorio = dir($path); 
        while($arquivo = $diretorio -> read()){ 
        if($arquivo != '.' && $arquivo != '..'){ 
         if(is_dir($path.$arquivo)) 
         { 
          //if subfolder 
          echo_directory_images($path.$arquivo.'/') 
         } 
         else{ 
         echo '<div href="#" class="list-group-item">'; 
         echo '<span class="close-button" secao="imagens">x</span>'; 
         echo "<img class='max-width' src='".base_url().$path.$arquivo."' />"; 
         echo '</div>'; 
         } 
        } 
        } 
} 

你可能想試用一下這個功能。我懷疑這是否能正常工作,但這肯定會給出一個關於如何遞歸調用文件夾和子文件夾的想法。

+0

@Artur你可能想放棄投票,如果它解決了你的問題。 –

0

試試這個:

function listDir($path) { 
     global $startDir; 
     $handle = opendir($path); 
     while (false !== ($file = readdir($handle))) { 
     if(substr($file, 0, 1) != '.') { 
      if(is_dir($path.'/'.$file)) { 
      listDir($path.'/'.$file); 
      } 
      else { 
      if(@getimagesize($path.'/'.$file)) { 

       /* 
       // Uncomment if using with the below "pic.php" script to 
       // encode the filename and protect from direct linking. 
       $url = 'http://domain.tld/images/pic.php?pic=' 
        .urlencode(str_rot13(substr($path, strlen($startDir)+1).'/'.$file)); 
       */ 

       $url='http://localhost/'.$path.'/'.$file; 
       substr($path, strlen($startDir)+1).'/'.$file; 

       // You can customize the output to use img tag instead. 
       echo "<a href='".$url."'>".$url."</a><br>"; 
       echo "<img class='max-width' src='".$url."' />"; 
      } 
      } 
     } 
     } 
     closedir($handle); 
} // End listDir function 

$startDir = "assets/imagens/"; 
listDir($startDir); 
0

您可以使用RecursiveDirectoryIterator

例如:

$directory = new RecursiveDirectoryIterator('assets/imagens'); 
$iterator = new RecursiveIteratorIterator($directory); 
foreach ($entities as $name => $entity) { 
    if ($entity->isDir()) { 
     continue; 
    } 

    $extension = pathinfo($name, PATHINFO_EXTENSION); 
    if ($extension == 'jpg' || $extension == 'png' || $extension == 'gif') { 
     echo '<img src="' . $entity->getPathname() . '" />'; 
    } 
} 
相關問題