Delete all files matching patter using PHP glob()

When you have the need to do a little house cleaning on your storage system programmatically you can use PHP’s glob() and array_map() functions to make easy work of this.

In this example, we are going to find all text files in “some/dir/” and unlink (aka: delete) them. The PHP glob() function gets us our list of files and then the array_map() function passes each index value to the callback function, in this case, the “unlink” system command.


array_map(callback, array)



The result of this

Author: Richard Soares

I’m a freelance software developer with more than 20 years’ professional experience in web development. I specialise in creating tailor-made, web-based systems that can help your business run like clockwork.