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


Insert Images from folder and subfolder php

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

我使用以下代码:

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

您可能想尝试一下此功能。我怀疑这是否会完全有效,但这肯定会给出有关如何递归调用文件夹和子文件夹的想法。

试试这个:

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

您可以使用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() . '" />';
    }
}