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.
You really have three choices here:
Option 1 - Works in 100% of cases. Might be tricky to maintain
echo '<a href="files/pdfs/'.$row['FILENAME_FROM_DATABASE'].'">PDF</a>';
// Could produce something like:
// <a href="files/pdfs/filename_v5.pdf">PDF</a>
Option 2 - Works in 99% of cases
echo '<a href="files/pdfs/filename.pdf?q='.microtime(true).'">PDF</a>';
Option 3 - Works in 99% of cases
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));
I would complete with a third option, by appending a dynamic parameter to the link that will download the file, ie :
<a href="http://host.com/my_file.pdf?t=<?php time(); ?>">My File</a>
That should bypass the cache.
In HTML5 you can force a browser not to cache for certain domains (or not to cache at all, or use cache if available and so on) - see https://developer.mozilla.org/en-US/docs/HTML/Using_the_application_cache
Add this to your <!doctype html><head>
-section :
<html manifest="my.cache">
create a file on your document root - my.cache
- containing the following :
CACHE MANIFEST
CACHE
# dont force any caching
NETWORK:
#force downloads form your site not to use cache
your-site.com
This forces that nothing is being cached.
If you have a path to your pdf-download, use that instead (so other files from your site except the PDF's will be cached)
Try this in a browser. Remember to clear the cache first! :) When you will discover that each PDF is downloaded, regardless of filename or headers.