Mobile app version of vmapp.org
Login or Join
Angie530

: Problem with browser caching images I'm sure many site admins must deal with this sort of problem all the time, but I don't have a really good and simple solution myself. It is what happens

@Angie530

Posted in: #Browsers #Cache #Css #Drupal

I'm sure many site admins must deal with this sort of problem all the time, but I don't have a really good and simple solution myself. It is what happens when you overwrite an existing image on the server. The site visitor's browser still displays the old one and must be refreshed to see the updated image (and stylesheet for that matter).

So is there any simple solution to trick the browser to refresh the cache when there's a new image using the old name? And yes, of course the obvious solution is to rename the image, but please bear with me on this and assume that's not always a good solution...

And this is for Drupal sites in case that bears on the question.

============================

ANSWER: Based on the answer below, I am now using this code:

<?php
echo 'niceimage.jpg?'.date("dmHi", filemtime('niceimage.jpg'));
?>


This automatically updates the image query string whenever the image-file is changed, based on the modification date of the image-file.

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Angie530

1 Comments

Sorted by latest first Latest Oldest Best

 

@Sarah324

One common way to do this is to append a query string to the name of the file like this:

/images/coolpic.png?randomchars


Appending the query string essentially "renames" the file so the browser will fetch the file. The query string is obviously useless to an image or stylesheet or other static files so it is ignored and won't break anything.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme