Quickly Get a List of Files in PHP with glob()

This is post is now quite old and the the information it contains may be out of date or innacurate.

If you find any errors or have any suggestions to update the information please let us know or create a pull request on GitHub

Perhaps not as well known as it should be, PHP’s glob() function is really quite powerful and exceptionally handy.

Need to get a list of files in a directory - try this:

$tools = glob('includes/tools/*');

Want to delete all files matching a specific pattern, try this:

array_map('unlink', glob('export_feeds/my_feed_*'));

Theres lots more tricks you can do.

One word of warning though - with great power comes great responsibility!

I would advise against using any kind of user input with this function.

Read more