0

我正在使用 PHP 生成一个 html 电子邮件,它以图表的形式向我的客户发送最新的统计信息。PHP 每次发送具有相同图像名称的统计信息时都会生成一个新图像,以防止高磁盘空间使用。现在我的问题是图像被缓存,因此向客户端显示旧图像而不是新图像。

我的 html 标头如下所示。

"From: Test <test@test.com>\n"
      // . "To: " . $contact . " <" . $email . ">\n"
       . "To: myemail@test.com\n"
       . "X-Confirm-Reading-To: test@test.com\n"
       . "Disposition-Notification-To: test@test.com\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/test@test.com"
       . "\nContent-Type: text/html\n\n";

如何强制邮件从服务器下载最新生成的图像?

4

2 回答 2

3

在 URL 中包含一些额外的内容,例如图形图像的时间戳

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

这样,只要图像发生变化,URL 就会发生变化。这不会阻止用户代理缓存它看到的内容,因此即使在服务器更新后它仍然可能显示旧图像。

因此,如果您想阻止用户代理实际缓存图像,请编写一个脚本,该脚本返回带有一些标头的图像以防止缓存......

$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);
于 2010-01-12T08:08:30.603 回答
1

让文件名本身包含时间戳。因此,不要覆盖旧图像,而是先将其删除(从而确保它真的消失了),然后用新图像名称替换它。

于 2010-01-12T08:12:18.270 回答