Take the 2-minute tour ×
Webmasters Stack Exchange is a question and answer site for pro webmasters. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 2 down vote accepted

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.

share|improve this answer
    
Thanks! Great idea. –  Michael D Jun 10 '11 at 2:01

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.