如何在PHP中使用scandir仅获取图像? [英] How to get only images using scandir in PHP?
本文介绍了如何在PHP中使用scandir仅获取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在使用时是否有任何方法只能获取扩展名为jpeg
,png
,gif
等的图像
Is there any way to get only images with extensions jpeg
, png
, gif
etc while using
$dir = '/tmp';
$files1 = scandir($dir);
推荐答案
您可以使用 glob
You can use glob
$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);
如果需要不区分大小写,可以将DirectoryIterator
与RegexIterator
组合使用,或者将scandir
的结果传递给array_map
并使用回调过滤所有不需要的扩展名.是否使用strpos
,fnmatch
或pathinfo
来获取扩展名取决于您.
If you need this to be case-insensitive, you could use a DirectoryIterator
in combination with a RegexIterator
or pass the result of scandir
to array_map
and use a callback that filters any unwanted extensions. Whether you use strpos
, fnmatch
or pathinfo
to get the extension is up to you.
这篇关于如何在PHP中使用scandir仅获取图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文