开发者

Using PHP can we search for a filename pattern in a directory

开发者 https://www.devze.com 2023-03-02 21:18 出处:网络
Am having a folder with millions of files in the server File name will be LMN20110305.txtpattern. If i am trying to search for files in april only am now reading directory

Am having a folder with millions of files in the server

File name will be LMN20110305.txt pattern.

If i am trying to search for files in april only am now reading directory

getting the filename and using substring am checking it is grea开发者_如何转开发ter than or less than of

20110401 and 20110430

In this case it will read entire directory and it is taking so many hours. Am thinking about checking create date also.

How will be the performance of glob — Find pathnames matching a pattern

Any one have any better suggestion?

Silly doubt! Is it possible to find the files without searching entire directory?


glob() will perform fine. It's in the standard library for a reason, use it!

It's my understanding that glob() maps almost directly to the glob() function in your system's C library, meaning that it will most likely be more efficient than anything you roll your own. The only limit is memory: if you run out of memory, you'll either need to give PHP more memory or reorganize.

If you run in to memory problems, how about organizing your files a little better? A million files in one folder is way too many. Organize them somehow. If your files are dated, try organizing them by year (and then maybe month), along the lines of:

basedir/
|---/2011
|---/2010
etc.


That's more a file system issue than PHP. PHP has no special methods for reading the directory other than via the usual system calls. You need to alter the storage pattern, some sort of tree structure appropriate to the distribution of data coming in.

0

精彩评论

暂无评论...
验证码 换一张
取 消