最好的方式来从一个特定的扩展名在php中过滤一个目录的文件 [英] Best way to get files from a dir filtered by certain extension in php

查看:118
本文介绍了最好的方式来从一个特定的扩展名在php中过滤一个目录的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:
目录中特定文件的PHP列表


所以现在我有一个目录,我得到一个文件列表

  $ dir_f =whatever / random /; 
$ files = scandir($ dir_f);

然而,它检索目录中的每个文件。我怎样才能以最有效的方式检索只有特定扩展名的文件,例如.ini。

解决方案

PHP有很好的功能帮助你只捕捉你需要的文件。它被称为 glob()


glob - Find匹配模式的路径名

以下是一个示例用法 -

  $ files = array(); 
foreach(glob(/ path / to / folder / *。txt)为$ file){
$ files [] = $ file;








Possible Duplicate:
PHP list of specific files in a directory
use php scandir($dir) and get only images!

So right now I have a directory and I am getting a list of files

$dir_f = "whatever/random/";
$files = scandir($dir_f);

That, however, retrieves every file in a directory. How would I retrive only files with a certain extension such as .ini in most efficient way.

解决方案

PHP has a great function to help you capture only the files you need. Its called glob()

glob - Find pathnames matching a pattern

Here is an example usage -

$files = array();
foreach (glob("/path/to/folder/*.txt") as $file) {
  $files[] = $file;
}

Reference -

这篇关于最好的方式来从一个特定的扩展名在php中过滤一个目录的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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