How to clear browser cache when re-uploading image with same filename, in php?

user15063 picture user15063 · Jun 29, 2010 · Viewed 32.5k times · Source

I have a listing of items, that also contains an image thumbnail. Sometimes I need to change the thumbnail image, but keep the filename the same. Is there a way to force the browser to download the newly uploaded image, instead of grabbing one from the browser cache?

This is very pertinent when people update their avatars, which causes confusion for some users, who keep seeing their old avatar, until they clear the browser cache, or restart the browser.

Answer

niallo picture niallo · Dec 18, 2010

Append a unique number to the end of the image src

ie

<?php

echo "<img src=../thumbnail.jpg?" . time() . ">";

do this only on the form for the updating avatar

works for me

btw, this works well for forcing updates of other static files

i.e. html for css files is

<link rel="Stylesheet" href="../css/cssfile.css?<?= md5_file('cssfile.css'); ?>" />