2

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

2 Answers 2

12

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.

3
  • 1
    what is a random query string, care to explain that part a little bit more please?
    – user188962
    Commented Dec 31, 2009 at 12:50
  • 1
    It's just a means of ensuring that the browser won't have seen that specific URL before and hence won't attempt to use a cached version. By adding time() to the end of the URL, @Jan is ensuring that the URL is unique. Commented Dec 31, 2009 at 12:54
  • we lose the advantage of cached element for second load Commented Jan 3, 2013 at 10:53
0

I've seen something like this before, but it was the other way around in that I was sending a file down from the server to the browser. The file name didn't change but the content should have, but it kept showing the original file's content.

I fixed it by changing a setting in php.ini :

session.cache_limiter = nocache