Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Possible to clear cache of browser with php code?

Tags:

html

php

I have a image upload tool written in php.

Users may chose a file, and it gets uploaded with a certain filename, then if the user regrets chosing that file they may click the file-input and upload another file instead, BUT THE FILENAME IS THE SAME, so the browser caches the first image uploaded. And instead of the second image the browsers display the first one instead, even though it's another image uploaded.

Kindof hard to explain...

How can I solve this?

Thanks


1 Answers

Either disable caching of images in your web server or append a random query string to the src of your image.

By "random query string" I mean that you append something that changes on every request to the URL of the image. Something like this:

<img src="http://www.example.com/image.jpg?<?php echo Time () ?>" />

Time () returns the current TIMESTAMP and will change on every request, and so the URL will always be different and thus forcing the browser to download the image every time.

like image 184
Jan Hančič Avatar answered Aug 06 '26 18:08

Jan Hančič