【问题标题】:Trying to show pictures from folder using while loop尝试使用while循环显示文件夹中的图片
【发布时间】:2016-02-21 02:22:47
【问题描述】:

我正在尝试使用 while 循环显示文件夹中的图片。我正在使用while循环生成html img标签,它应该生成不超过5个带有填充src参数的img标签,具体取决于目录中有多少文件,但它会生成另外两个带有空src参数的img标签。

我的代码是:

if (is_dir($directory)) {
   if ($dh = opendir($directory)) {
        while (($file = readdir($dh)) !== false) {
            echo '<img style="width:100px; height:100px" src="/resource/news_img/'.$Row['dimg'].'/'.$file.'">';
        }
        closedir($dh);
    }
}

我不明白为什么会这样。我该如何解决?

【问题讨论】:

    标签: php


    【解决方案1】:

    readdir 还会列出... 路径(当前和父目录),您需要将它们过滤掉。

    while (($file = readdir($dh)) !== false) {
        if ($file != "." && $file != "..") {
            // ...
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-14
      • 1970-01-01
      • 1970-01-01
      • 2011-05-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多