PHP-查找目录中与特定字符串匹配的所有所有文件,并将其放入数组中 [英] PHP - find all all files within directory that match certain string and put in array

查看:25
本文介绍了PHP-查找目录中与特定字符串匹配的所有所有文件,并将其放入数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在客户上传的服务器上有一个图像目录.我需要能够获取与特定字符串或项目代码匹配的所有文件,并将它们放入数组中.文件名和扩展名始终可以变化,但是每个文件的文件名中始终具有8位数字的项目代码.例如,在我的目录中,我有:

I have a directory of images on a server that customers have uploaded. I need to be able to get all files that match a certain string or item code and put them inside an array. Filenames and extensions can always vary but each file will always have an 8 digit item code in the filename. So for instance say in my directory i have:

/images/

62115465.jpg
62115465-02.jpg
62115465-07.jpg
13452766.png
56773392.jpeg
56773392-avatar.jpg

我希望能够提取出与8位项目代码匹配的所有文件,因此:

I want to be able to pull out all the files that match the 8 digit item code so:

//all images that have 62115465 in the file name would give me

62115465.jpg
62115465-02.jpg
62115465-07.jpg

//or all images that have 56773392 in the file name would give me

56773392.jpeg
56773392-avatar.jpg

然后将它们放在这样的数组中:

and then want them in an array like so:

$all_files = array(
  '62115465.jpg',
  '62115465-02.jpg',
  '62115465-07.jpg'
);

我尝试使用下面的 glob()函数,该函数可以匹配某些文件,例如 62115465.jpg ,但没有使用拾取其他2个文件-02 -07 标签

I tried using the glob() function as below which can match some files like the 62115465.jpg but doesnt pick up the 2 other files with the -02 and -07 tags

$files = glob('62115465.'.*');

推荐答案

glob('62115465*');

请注意删除..glob()本质上是在命令提示符下进行类似 dir * .txt 的操作.

note the removal of the .. glob() essentially replicates doing something like dir *.txt at a command prompt.

这篇关于PHP-查找目录中与特定字符串匹配的所有所有文件,并将其放入数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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