How to define multiple patterns in php glob()

不打扰是莪最后的温柔 提交于 2019-12-07 12:28:41

问题


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().


回答1:


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



来源:https://stackoverflow.com/questions/38899453/how-to-define-multiple-patterns-in-php-glob

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