从文件夹和子文件夹php插入图像

时间:2014-12-26 07:18:48

标签: php wamp server

我试图在html中插入文件夹上的所有图像" assets / imagens /"以及任何子文件夹。我还需要帮助来验证不回显子文件夹。

我正在使用以下代码:

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

3 个答案:

答案 0 :(得分: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>';
                       }
                    }
                  } 
}

您可能想尝试一下这个功能。我怀疑这是否可以正常工作,但这肯定会给出一个关于如何递归调用文件夹和子文件夹的想法。

答案 1 :(得分: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 );

答案 2 :(得分: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() . '" />';
    }
}