Loop over files with GLOB

In PHP we have the possibility of looping over files in a directory that matches a certain pattern.

This gives us the option to get all files with a certain file extension.

Before continuing, if you have a large amount of files or a lot of big files, you might experience some memory problems.

A simple way of using it, can be seen below:

foreach (glob("/my/file/dir/*.json") as $filename) {
    //Do something with the file.

The above example will loop over all json files in the /my/file/dir/ directory and supply each filename.

The documentation for the function can be found here: http://php.net/manual/en/function.glob.php, which includes more advanced examples.



Bjarne Bonde