Forum Moderators: coopster

Message Too Old, No Replies

reloading change images

         

mynot

12:16 am on Jul 2, 2011 (gmt 0)

10+ Year Member



Hi,
I have a php script which displays an existing image from the server, allows the user to Browse their own pc to select a new image, uploads the new image (giving it the same name and path as the old image) then re-displays the page:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<META http-equiv="Pragma" content="no-cache">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
etc
</head>
<body>
<?php
$currentImage=#*$!xx
if (isset($_POST['submit']){
upload file
}
?>
<form etc
</form>
</body>
</html>

The problem is the old image isn't replaced until I clear the cache. I thought the two "meta" tags were supposed to solve this problem, but they don't seem to.
Any help appreciated.
Tony

penders

1:14 am on Jul 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You are probably better off setting the actual HTTP cache control headers rather than the META tags. But this will mean that the page is never cached.

You could perhaps add a random querystring to the end of the image url to prevent the cached image from being used...? Perhaps base this on the timestamp of the saved file? Like so...
<img src="..../mypic.jpg?123456">


...and welcome to WebmasterWorld :)

mynot

9:13 am on Jul 4, 2011 (gmt 0)

10+ Year Member



penders,
Thanks. I have added the HTTP cache control. The (cut down) script is now:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<?php ob_start();
header("Expires: Fri, 30 Oct 1998 14:19:41 GMT"); //any date in the past will do
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("cache-control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
?>


<html>
<head>
<title>test-reload</title>
<META http-equiv="Pragma" content="no-cache">
<META HTTP-EQUIV="CACHE-CONTROL" CONTENT="NO-CACHE">
<LINK REL="stylesheet" type="text/css" href="memberarea.css">
</head>

<body>
<?php
include ("L_Scripts.php");
include ("scripts.php");
include ("constants.php");
$currImage="../images/chorale_logo.png";
//------------------------------------------
if (isset($_POST["submit"])) {
if(!move_uploaded_file($_FILES['newfile']['tmp_name'], $currImage));
}
//------------------------------------------------------------------------
?>
<form title='CMS Tips' method='POST' action='' enctype="multipart/form-data">
<p>
<table>
<tr>
<td align='center'>
<img src='<?php echo $currImage;?>' border='0'>
</td>
<td valign='top'>
To change this, browse new logo file and click "Submit":<p>
<input type='file' name='newfile' size='60'><br>
<center><input type='submit' name='submit' value='Submit'></center>
</td>
</tr>
</table>
</form>
</body>
</html>
<?php ob_end_flush(); ?>

After I click "Submit" it still shows the old image even though file is actually changed on the server (localhost).
Thanks
Tony

rocknbil

4:35 pm on Jul 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Use the query string method penders' suggested . . . it is the only way around browser caching that is reliable other than saving the file as a new file name. Generate any random sequence of numbers and letters and append it to image file name with ?.

penders

9:50 pm on Jul 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">  
<?php ob_start();
header("Expires: Fri, 30 Oct 1998 14:19:41 GMT"); //any date in the past will do
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("cache-control: no-store, no-cache, must-revalidate");
header("Pragma: no-cache");
?>
:


Doesn't this generate an error?

Generate any random sequence of numbers and letters and append it to image file name with ?


The thought behind using an id based on the timestamp was that presumably the image should be cached at other times?