Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to define multiple patterns in php glob()

Tags:

php

glob

My code to get all images from a directory

$dirname = "uploads/";

$images = glob("{$dirname}*.png, {$dirname}*.jpeg, {$dirname}*.jpg, {$dirname}*.gif");

foreach($images as $image) {

    echo "<img src='{$image}' class='files_main'>";

}

This works for one type of image but fails with multiple please give the syntax of defining multiple patterns in the glob().

like image 455
rayees Avatar asked Aug 11 '16 14:08

rayees


1 Answers

You can use the GLOB_BRACE constant

GLOB_BRACE - Expands {a,b,c} to match 'a', 'b', or 'c'

e.g.

$dirname = 'uploads/';
glob("$dirname*.{png,jpeg,jpg,gif}", GLOB_BRACE);

See: http://php.net/manual/en/function.glob.php

like image 160
iainn Avatar answered Sep 22 '22 00:09

iainn