I\'m creating an input form that allows users to upload an image. Once it has been submitted the user is returned to the form with a conformation bit at the top.
An
In PHP you can send a random number or the current timestamp:
<img src="image.jpg?<?=Date('U')?>">
or
<img src="image.jpg?<?=rand(1,32000)?>">
The trick here is to tell the browser to not cache the content. You can do this by putting these statements before ANYTHING else (no whitespace or anything!)
<?php
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
Depending on how you're loading the image, you can also do a javascript refresh of the image using jQuery.
Add the modified date to the end of the image as a query.
<img src="/images/photo.png?=1239293"/>
In PHP
<img src="/images/photo.png?=<?php echo filemtime($filename)?>"/>
You could replace the current image by a new one using JavaScript:
<div id="imageHolder">this is where the image will show</div>
<script type="text/javascript">
function myfunction(){
document.getElementById("imageHolder").innerHTML="<div id=\"imageHolder\"><img src='imagefolder/imagename.jpg' alt=''/></div>";
}</script>
You need to trigger the function myfunction after completing the upload of course ...
This worked great for me:
image.jpg?=<?php echo rand() . "\n";?>