11

I have built a small PHP/MySQL internal app to host and sort documents. All works perfectly until it comes to updating a file, in this case a .PDF file. When a user updates the .PDF the new file is on the server as expected and the older version deleted. A user is getting the new version providing they never opened the old version.

Now the problem.... If a user has opened the older version of the .PDF at some point in the past they do not get the newer version when a link is clicked to view the document even though its only the new version actually physically on the server.

I'm guessing that Google Chrome Browser is caching the older version of the PDF somewhere. How can I get around this? Due to the amount of users and the number of times a day some of the documents are updated asking users to manually clear any cache is not practical.

4

3 回答 3

23

你在这里真的有三个选择:

  1. 每次更新时更改文件名
  2. 始终使用 GET 参数生成 HREF
  3. 发送标头信息,告诉浏览器始终从服务器下载新内容

选项 1 - 适用于 100% 的情况。可能很难维护

echo '<a href="files/pdfs/'.$row['FILENAME_FROM_DATABASE'].'">PDF</a>';

// Could produce something like:
// <a href="files/pdfs/filename_v5.pdf">PDF</a>

选项 2 - 适用于 99% 的情况

echo '<a href="files/pdfs/filename.pdf?q='.microtime(true).'">PDF</a>';

选项 3 - 适用于 99% 的情况

header("Pragma: public");
header("Cache-Control: maxage=1"); // <-- important
header('Expires: '.gmdate('D, d M Y H:i:s', time()+1).' GMT');
header('Content-type: application/pdf');
exit(file_get_contents(PATH_TO_PDF_FILE));
于 2013-10-07T12:43:01.333 回答
2

在 HTML5 中,您可以强制浏览器缓存某些域(或根本不缓存,或者在可用时使用缓存等等) - 请参阅https://developer.mozilla.org/en-US/docs/HTML/使用_the_application_cache

将此添加到您的<!doctype html><head>-section :

<html manifest="my.cache">

在您的文档根目录上创建一个文件my.cache- 包含以下内容:

CACHE MANIFEST  
CACHE  
# dont force any caching 
NETWORK:
#force downloads form your site not to use cache
your-site.com

这迫使没有任何东西被缓存。

如果您有 pdf 下载的路径,请改用该路径(以便您站点中的其他文件(PDF 除外)将被缓存)

在浏览器中试试这个。记得先清除缓存!:) 当您发现每个 PDF 都已下载时,无论文件名或标题如何。

于 2013-10-07T12:55:20.313 回答
-2

我将完成第三个选项,通过将动态参数附加到将下载文件的链接,即:

    <a href="http://host.com/my_file.pdf?t=<?php time(); ?>">My File</a>

那应该绕过缓存。

于 2013-10-07T12:48:21.143 回答