php foreach glob multiple extensions

懵懂的女人 提交于 2019-12-11 02:58:57

问题


There has got to be a better way to write this:

<?php $imagecounter = "no";
    foreach (glob("images/*.jpg") as $image)    {
    $imagecounter = "yes";
    }
    foreach (glob("images/*.png") as $image)    {
    $imagecounter = "yes";
    }
    foreach (glob("images/*.gif") as $image)    {
    $imagecounter = "yes";
    }
    if ($imagecounter == "yes"){Create gallery}?>

That folder might have zip or pdf files too that should not create a gallery


回答1:


if(glob("images/*.{jpg,png,gif}", GLOB_BRACE))
{
  //create gallery
}

And that's about it :)




回答2:


I think you want to remove foreach looping and check that in images folder any extension from "jpg,png,gif". so you can use ternary operator. Here is sample code.

$imagecounter = glob("img/*.jpg")?'yes':$imagecounter;
$imagecounter = glob("img/*.png")?'yes':$imagecounter;
$imagecounter = glob("img/*.gif")?'yes':$imagecounter;

if ($imagecounter == "yes"){Create gallery}

?> If i assume wrong, let me know mate.



来源:https://stackoverflow.com/questions/23969187/php-foreach-glob-multiple-extensions

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!