Let’s say you’re working on a WordPress file that has to import data from a very large file. To avoid PHP timeouts, it helps to separate the files and then import then in pieces (or in chunks, because that word is more fun :).

But to do that, it implies you’re capable of taking a file and splitting it into some smaller files.

When you do this, I assume that each smaller file will follow a particular naming convention (like file_0.txt, file_1.txt, and so on).

Read All Files of a Certain Type

So when it comes time to read each file, how do you do it?

Read All Files

Whenever I’m working in a situation like this, there are always two challenges (they are the same every time, so when you solve it once, you’ve solved it).

  1. How do I avoid PHP timeouts?
  2. How do I read individual files?

Avoiding PHP timeouts can be done by reading a single file during an Ajax request. But to do that, you have to know how to read all files of a particular naming convention.

Fortunately, PHP provides the glob function which makes this much easier.

The glob() function searches for all the pathnames matching pattern according to the rules used by the libc glob() function, which is similar to the rules used by common shells.

First, though, you need to know the directory in which the files reside.

In the following code, I’m assuming that you have an uploads directory in the same directory of the current file.

After that, I’m looping through the directory and adding the path to all of the files that match a certain naming convention.

Once done, I’m returning the array of files to the function’s caller. From here, I can then read the file, parse the information, or do whatever needs to be done.