Automated cache-buster on images in PHP

I have a particular site I manage where one particular image (a grid of sponsors) gets updated quite regularly.

Rather than edit my source code each time I upload a new image, I thought I’d let PHP do the work for me.

<img class="img-responsive" src="images/sponsors/silver.jpg?<?= date('U',filemtime('images/sponsors/silver.jpg')) ?>" />

Now, every time I upload a new image, replacing silver.jpg, it will automatically update the image in the users’ cache.

Just a silly little time saver.

Note: I wouldn’t do this on every image on a site since it means an extra hit to the filesystem. That could mean a performance drop if a site is checking the filemtime of 100 images. In my case, it’s just a single image, so it’s okay.