glob php - 从目录中返回所有类型的图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17309554/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:39:48  来源:igfitidea点击:

glob php - return all types of images from directory

phpimageincludecarouselglob

提问by Luke Cottingham

I have a php function to grab images from a folder for a carousel and build the carousel on those images. I have a thumbnail folder and a main img folder.

我有一个 php 函数可以从一个文件夹中抓取图像作为轮播,并在这些图像上构建轮播。我有一个缩略图文件夹和一个主 img 文件夹。

My question is, how can I modify this code or what to add in order to recognise more image file types and not only jpg. I want to support png, gif and jpg.

我的问题是,如何修改此代码或添加什么以识别更多图像文件类型,而不仅仅是 jpg。我想支持 png、gif 和 jpg。

My code is below:

我的代码如下:

<?php $thumbs = glob("img/thumb/*.jpg"); ?>
    <?php
    if(count($thumbs)) {
      natcasesort($thumbs);
      foreach($thumbs as $thumb) {
      ?>
          <li class="item">
              <a class="fancybox" rel="gallery1" href="img/large/<?php echo basename($thumb) ?>">
                  <img src="<?php echo $thumb ?>" width="100%" alt="" />
              </a>
          </li> 

    <?php
        }} else {
          echo "Sorry, no images to display!";
        }
    ?>

回答by

Change this line :

改变这一行:

glob("img/thumb/*.jpg")

to

glob("img/thumb/*.{jpg,png,gif}", GLOB_BRACE)

It's important to have no spaces in the list of endings.

结尾列表中没有空格很重要。