php - 如何在 PHP 中使用 scandir 只获取图像?

标签 php directory-structure file-extension scandir image-file

有没有办法在使用时只获取扩展名为jpegpnggif等的图片

$dir    = '/tmp';
$files1 = scandir($dir);

最佳答案

您可以使用 glob

$images = glob('/tmp/*.{jpeg,gif,png}', GLOB_BRACE);

如果您需要不区分大小写,您可以使用 DirectoryIterator 结合 RegexIterator 传递 scandirarray_map 并使用过滤任何不需要的扩展名的回调。是否使用 strposfnmatchpathinfo 来获取扩展取决于您。

关于php - 如何在 PHP 中使用 scandir 只获取图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3226519/

相关文章:

php - fputcsv 将结果打印到屏幕而不是 csv

php - 能够在某些页面上更改 disqus_identifier

git - 为什么 git 将对象存储在具有哈希值前两个字符的目录中?

vb.net - 从文件资源管理器打开我的应用程序中的文件

powershell - 为 PowerShell Core 脚本使用 .pwsh 而不是 .ps1 扩展名?

php - catch 不工作

在单独的进程中运行测试时,PHPUnit 无法生成代码覆盖率

php - 访问root以外文件的方法

c++ - 多项目 cmake 不工作

dll - 如何判断文件是EXE还是DLL?