【问题标题】:Load all images from folder从文件夹加载所有图像
【发布时间】:2016-05-28 00:59:48
【问题描述】:

我正在尝试将文件夹中的图片插入到使用 javascript 制作的图库中。

我正在使用 php 循环插入所有图像,但它不起作用。 代码如下:

<div id="mygallery">
            <?php $directory="img/portfolio";
            $images=glob($directory . "*.jpg");
            $swipebox = "swipebox";
            foreach($images as $image) {
                echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . "src=". $image . "/>";
            }?></div>

【问题讨论】:

  • 试试这个 $images=glob($directory.'/*');

标签: javascript php image directory image-gallery


【解决方案1】:

这是正确的代码:

<div id="mygallery">
<?php
    $directory="img/portfolio/";
    $images=glob($directory . "*.jpg");
    $swipebox = "swipebox";
    foreach($images as $image) {
        echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . " src=". $image . " /></a>";
    }
?>
</div>

以下是您在代码中犯的错误:
1. 需要在图片目录路径后第3行后面加上斜杠'/'
2. 需要在第 7 行的srcalt 属性之间放置空格

【讨论】:

    【解决方案2】:

    这行得通,您需要在“src”之前留一个空格并关闭 href。

    <?php 
        $directory="img/portfolio";
        $images=glob($directory . "*.jpg");
        $swipebox = "swipebox";
        foreach($images as $image) {
            echo " <a href=" .$image." class=".$swipebox."> <img alt=" . $directory . " src='". $image . "' /> </a>";
    }?>
    

    【讨论】:

      【解决方案3】:

      试试下面,

      <?php
          $allImageFiles = scandir('img/portfolio');
          $swipebox = "swipebox";
      
      
          foreach($allImageFiles as $key=>$image) {
              echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . "src=". $image . "/>";
      
          }
      ?>
      

      【讨论】:

        【解决方案4】:

        上面的 cmets 提到了两件事。 1.在路径中添加斜杠(/) 2. src和alt之间加空格

        glob 函数返回的列表是匹配给定模式的文件名/目录名。您必须在图像源中加入路径。试试下面的代码。它应该可以工作。

        <div id="mygallery">
        <?php
            $directory="img/portfolio/";
            $images=glob($directory . "*.jpg");
            $swipebox = "swipebox";
            foreach($images as $image) {
                echo "<a href=" .$image." class=".$swipebox."> <img alt=" . $directory . " src=". $directory .$image . " /></a>";
            }
        ?>
        </div>
        

        【讨论】:

          猜你喜欢
          • 2011-12-09
          • 2017-12-03
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-06-24
          • 1970-01-01
          • 2022-07-06
          • 2012-07-28
          相关资源
          最近更新 更多