Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从php文件夹和子文件夹插入图像_Php_Wamp_Server - Fatal编程技术网

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

从php文件夹和子文件夹插入图像,php,wamp,server,Php,Wamp,Server,我正在尝试将文件夹“assets/imagens/”和任何子文件夹中的所有图像插入html。我还需要帮助来验证是否不回显子文件夹 我使用下面的代码: $path = "assets/imagens/"; $diretorio = dir($path); while($arquivo = $diretorio -> read()){ if(

我正在尝试将文件夹“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=“资产/imagens/”;
$diretorio=dir($path);
而($arquivo=$diretorio->read()){
如果($arquivo!='.&&$arquivo!=''.')){
回声';
回声“x”;
回声“;
回声';
}
} 
$diretorio->close();
$path=“资产/imagens/”;
echo_目录_图像($path);
函数echo_目录_图像($path)
{
$diretorio=dir($path);
而($arquivo=$diretorio->read()){
如果($arquivo!='.&&$arquivo!=''.')){
if(is_dir($path.$arquivo))
{
//if子文件夹
echo_目录_图像($path.$arquivo.'/'))
}
否则{
回声';
回声“x”;
回声“;
回声';
}
}
} 
}
您可能想尝试一下这个功能。我怀疑这是否能完全起作用,但这肯定会让您了解如何递归调用文件夹和子文件夹。

尝试以下方法:

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 );
函数listDir($path){
全球$startDir;
$handle=opendir($path);
while(false!=($file=readdir($handle))){
if(substr($file,0,1)!='。){
if(is_dir($path.'/'.$file)){
listDir($path.'/'.$file);
}
否则{
if(@getimagesize($path.'/'.$file)){
/*
//如果与下面的“pic.php”脚本一起使用,则取消注释
//对文件名进行编码并防止直接链接。
$url='1http://domain.tld/images/pic.php?pic='
.urlencode(str_rot13(substr($path,strlen($startDir)+1)。'/'.$file));
*/
$url='1http://localhost/“.$path.”/“.$file;
substr($path,strlen($startDir)+1)。“/”.$file;
//您可以自定义输出以改用img标记。
回声“
”; 回声“; } } } } closedir($handle); }//结束listDir函数 $startDir=“资产/imagens/”; listDir($startDir);
您可以使用

例如:

$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() . '" />';
    }
}
$directory=new RecursiveDirectoryIterator('assets/imagens');
$iterator=新的递归迭代器($directory);
foreach($name=>$entity形式的实体){
如果($entity->isDir()){
持续
}
$extension=pathinfo($name,pathinfo\u extension);
如果($extension=='jpg'| |$extension=='png'| |$extension=='gif'){
回显“getPathname()”/>;
}
}

好奇-您当前遇到的问题到底是什么?您需要检查当前的$arquivo是文件夹还是文件,如果文件夹需要对该子文件夹递归执行相同的过程。@Artur如果它解决了您的问题,您可能想放弃投票。
$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() . '" />';
    }
}