如何在PHP中使用scandir仅获取图像? [英] How to get only images using scandir in PHP?

查看:83
本文介绍了如何在PHP中使用scandir仅获取图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使用时是否有任何方法只能获取扩展名为jpegpnggif等的图像

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);

如果需要不区分大小写,可以将DirectoryIteratorRegexIterator 组合使用,或者将scandir的结果传递给array_map并使用回调过滤所有不需要的扩展名.是否使用strposfnmatchpathinfo来获取扩展名取决于您.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆