【问题标题】:output files from a second directory in same php function从同一php函数中的第二个目录输出文件
【发布时间】:2010-03-03 20:43:42
【问题描述】:

我有以下代码可以从目录 im/ 输出图像,如何调整它以从另一个名为 out/ 的目录中输出图像(例如)?至于在当前标签下回显另一个 img 标签?

<?php
    $imgDir = "im/";

    $images = scandir($imgDir); 
    $ignore = array( ".", ".." ); 

    natsort($images);

    foreach($images as $file)
       {
    if(!in_array($file, $ignore))
       {
    echo "<div id=\"slideWrapper\">\n";
    echo "<img src=\"im/$file\" width=\"1000\" height=\"683\" alt=\"$files\" />\n";
    echo "</div>\n";
    };
    }
?>

【问题讨论】:

    标签: php directory scandir


    【解决方案1】:

    我会这样做 - 用 $imgDir 交换一个数组。 $images 数组现在包含路径和文件名。

    <?php
        $imgDirs = array("im/", "out/");
        $images = array();
    
        // take one of the given directories
        foreach($imgDirs as $imgDir)
        {
           // open a directory reader for this given directory
           $dh = opendir($imgDir);
           // read a single filename of this directory (stop loop if there is no more file)
           while (false !== ($filename = readdir($dh))) 
           {
               // ignore '.' and '..'
               if($filename != '.' && $filename != '..')
               {
                  // add the directory and filename to the images array
                  // all images, regardless of the folder, are stored in one array :)
                  $images[] = $imgDir . $filename;
               }
           } 
           closedir($dh);
        }
    
        natsort($images);
    
        // loop for every image
        foreach($images as $file)
        {
           // for every img, echo a div-img-/div-combination
           echo "<div id=\"slideWrapper\">\n";
           echo "<img src=\"$file\" width=\"1000\" height=\"683\" alt=\"$file\" />\n";
           echo "</div>\n";
        }
    ?>
    

    【讨论】:

    • 如果您想从文件中检索图像尺寸,请使用:php.net/manual/de/function.getimagesize.php
    • 好的,谢谢!但是,如果我要像这样输出 osthing:
      因为我需要将每个目录的一个图像分成两个单独的img 标签?
    • 使用上面的代码,你会得到每个图像的 div-img-/div-combination,不管它们在哪个文件夹中。我添加了 cmets 以进行澄清。您需要对图像进行分组吗?
    • 是的!并感谢您的澄清。我需要一个 div-img(from im/)-img(fom out/)-div。例如,对于一个文件夹中名为 x.jpg 的每个图像,来自另一个文件夹的图像名称 x1.jpg 会在下面回显。
    • 并且保证文件夹1中的每一个x.jpg,文件夹2中都有一个x1.jpg存在?
    【解决方案2】:

    相信这会满足您的需求。

    <?php
        $imgDir = 'im/';
        $imgDir2 = 'out/';
        $images = array();
    
        // open a directory reader for the first directory
        $dh = opendir($imgDir);
        // read a single filename of this directory (stop loop if there are no more files)
        while (false !== ($filename = readdir($dh))) 
        {
            // ignore '.' and '..'
            if($filename != '.' && $filename != '..')
            {
               // add the directory and filename to the images array
               $images[] = $filename;
            }
        }
        closedir($dh);
    
        natsort($images);
    
        // loop for every image
        foreach($images as $image)
        {
           // for every img, echo a div-2-images-div-combination
           echo '<div id="slideWrapper">';
           echo '<img src="'.$imgDir . $image.'" width="1000" height="683" alt="'.$image.'" />';
           echo '<img src="'.$imgDir2 . $image.'" width="1000" height="683" alt="'.$image.'" />';
           echo '</div>';
        }
    ?>
    

    我建议使用单引号,因为您不必转义普通引号,而且速度更快:)

    【讨论】:

    • 感谢您的时间心腹,慢慢掌握这一切!
    猜你喜欢
    • 2020-12-12
    • 1970-01-01
    • 1970-01-01
    • 2016-01-31
    • 1970-01-01
    • 2020-08-31
    • 2011-09-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多