Can PHP clear the browser cache and refresh?

I have a script which allows a visitor to the web site to upload an image. Since this replaces an earlier image, it gets given the same name (desirably). The upload, and the renaming happen OK. But when all is committed to the database and the visitor is shown the result, the old image is still visible. Clearing the browser cache and refreshing the screen (manually) shows the new image.

Is there a way I can get PHP (or JS, I suppose) to do this automatically as part of the script ?

one way to force a reload of the image every time the page is loaded is to append a unique query string to the filename of the <img>'s src. So even though the filename will be the same, the unique query string will be different thus forcing the browser to download the image again because the url for the image will be different on each page load.

I normally use the current date/time for the unique query string.

Thanks. That’s a good suggestion. At first sight I wasn’t sure if I could do that, because the file name is being drawn from a database (one reason for retaining the same image filename). But it should be possible to append a query string at some later point. I’ll look into it.

you’re welcome :slight_smile:

I would set the query string on the server so that you don’t have to worry about javascript disabled browsers.

something along this line:


$today = date("F j, Y, g:i a");
echo '<img src="pic1.jpg?myPic='.urlencode($today).'" alt="" />';

in your case you would need to build the string for the img src by appending the current date/time to the file name of the image that was retrieved from the database

Thanks for the extra pointers. I’ve got it set up much as you suggest, and it seems to be working a treat (why would it not ?). A bit more testing, though, to make sure there’s no unexpected consequence.

To make matters more exciting, it’s all within a loop so I’ve got to make sure the date/time variable gets refreshed every pass, or it will fail in it’s purpose. But that’s just detail. The main problem is resolved. Thanks again.