views:

158

answers:

2

I'm using PHP to generate an html email which sends my clients latest stats in graph form. PHP generates a new image everytime it sends out stats with the same image name to prevent high disk space usage. Now my problem is the image gets cached thus displaying the old image to the client instead of the new image.

My html headers look as follows.

"From: Test <[email protected]>\n"
      // . "To: " . $contact . " <" . $email . ">\n"
       . "To: [email protected]\n"
       . "X-Confirm-Reading-To: [email protected]\n"
       . "Disposition-Notification-To: [email protected]\n"
       . "MIME-Version: 1.0\n"
       . "Content-Type: multipart/mixed;"
       . ' boundary="PAA08673.1018277622/www.test.com"'
       . "\nSubject: Stats for $name\n\n"
       . "This is a MIME-encapsulated message\n\n"
       . "--PAA08673.1018277622/[email protected]"
       . "\nContent-Type: text/html\n\n";

How can I force the mail to download the latest generated image from the server?

+3  A: 

Include something extra in the URL, like the timestamp of the graph image

<img src="http://example.com/graphs/graph.png?t=1263283697"&gt;

That way, the URL changes whenever the image does. This won't stop the user-agent from caching what it sees, so it still might show an old image even after the server updates.

So, if you want to stop the user-agent from actually caching the image, then write a script which returns the image with some headers to prevent caching....

$file="graph.png";
$size=filesize($file);

header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("Content-Length: $size");
header("Content-Type: image/png");

readfile($file);
Paul Dixon
+1  A: 

Have the filename itself include a timestamp. So instead of overwriting the old image, you delete it first (thus insuring it's really gone) and replace it with the newer image with the newer image name.

Anthony