2014-12-26 3 views
1

"assets/imagens /"폴더 및 모든 하위 폴더에있는 모든 이미지를 html에 삽입하려고합니다. 또한 하위 폴더를 반향하지 않도록 확인하는 데 도움이 필요합니다. 아래의 코드를 사용하여폴더 및 하위 폴더에서 이미지 삽입 ph

임 :

    $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

호기심 - 경험 you'er 현재 문제가 정확히 무엇인가? – 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

이 시도 : 당신은 RecursiveDirectoryIterator을 사용할 수 있습니다

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

. 예를 들어

:

$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() . '" />'; 
    } 
} 
관련 문제