This question already has an answer here:
这个问题在这里已有答案:
- PHP file listing multiple file extensions 2 answers
PHP文件列出多个文件扩展名2个答案
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!";
}
?>
1 个解决方案
#1
57
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.
在结尾列表中没有空格很重要。
#1
57
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.
在结尾列表中没有空格很重要。