PHP 的 glob() 可以以不区分大小写的方式查找文件吗?
我希望目录中的所有 CSV 文件,因此我使用
glob('my/dir/*.CSV')
This,但是找不到带有小写 CSV 扩展名的文件。
我可以使用
glob('my/dir/*.{CSV,csv}', GLOB_BRACE);
但是有没有办法允许所有混合大小写版本?或者这只是 glob()
的限制?
I want all CSV files in a directory, so I use
glob('my/dir/*.CSV')
This however doesn't find files with a lowercase CSV extension.
I could use
glob('my/dir/*.{CSV,csv}', GLOB_BRACE);
But is there a way to allow all mixed case versions? Or is this just a limitation of glob()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
要使其与所有扩展一起使用,请使用:
To make it work with all extensions use:
Glob 模式支持字符范围:
Glob patterns support character ranges:
你可以这样做
You could do this
glob('my/dir/*.[cC][sS][vV]')
应该可以做到。是的,有点丑。glob('my/dir/*.[cC][sS][vV]')
should do it. Yeah it's kind of ugly.您还可以在选择所有文件后过滤掉这些文件,
从性能角度考虑,这可能不是最佳选择,例如,如果文件夹中有 100 万个非 csv 文件。
You can also filter out the files after selecting all of them
performance wise this might not be the best option if for example you have 1 million files that are not csv in the folder.
该代码适用于我仅获取图像且不区分大小写。
图像列表:
也许它看起来很难看,但你只需要声明 $imageOnly 一次,就可以在需要的地方使用它。您还可以声明 $jpgOnly 等。
我什至制作了一个函数来创建此模式。
This code works for me to get images only and case insensitive.
imgage list:
Perhaps it looks ugly but you only have to declare the $imageOnly once and can use it where needed. You can also declare $jpgOnly etc.
I even made a function to create this pattern.
您可以编写自己的不区分大小写的 glob。这是我写的个人网络图书馆:
You can write your own case insensitive glob. This is from a personal web library I write:
我听说有一个函数可以这样使用:
尝试一下是否适合您!
I heard about a function that can be used like this:
Try if that works for you!
来到此链接获取具有多个文件的 glob。虽然它对 OP 没有帮助,但可能会对其他最终来到这里的人有所帮助。
Came to this link for glob with multiple files. Although it doesn't help with OP, it may help others who end up here.
根据 Alex 的提示,这通常会有所帮助:
其中
$d
是目录,$e
是扩展名。Building on Alex's tip this could help generally:
where
$d
is the directory and$e
is the extension.添加到 Ignacio 答案中,如果您想使用不区分大小写的变量,您可以通过将变量拆分为字节并自动准备字符范围来实现:
只需更改 $dir 和 $var 就可以了。
Adding to Ignacio answer, if you want to use a case insensible variable instead, you can do it by splitting the variable into bytes and preparing char ranges automatically:
Just change $dir and $var and you're ready to go.