PHP – Delete files by pattern

Martin Zeller php 1 Comment

If you want to delete files by pattern (e.g. “*.txt”) in a directory, you can do this with the php function glob!

$files = glob('/path/to/files/*.txt'); // finds all txt files in the directory /path/to/files/
foreach($files as $file)
  unlink($file);

With this method you can also delete all files of a directory – with pattern *.*

Comments 1

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.