ak85 ak85 - 8 months ago 38
PHP Question

PHP Delete all files that contain a given string

I have a basic cache system setup which saves a file based on the parameters in the url so that if that page gets viewed again it accesses the static file. eg if my url is


I have a file located in a cache folder called id=1.html


currently this is cached for a specified amount of time however I want it to always use the cached file unless the page is updated.

So I implemented the below php code.

unlink('../' . $gender . '/cache/id=' . $_POST['id'] . '.html');

this works fine however, some times there are additional parameters in my url. So currently I have the below files in my cache folder


But when I save my content only female/cache/id=1.html is removed.

How would I go about removing any file in this folder with the id=1


You could use glob:

foreach (glob("female/cache/id=1*.html") as $filename) {

Where the asterisk * matches all the variations of the filename.