Jump to content

Temp files


laPistola

Recommended Posts

Hello

 

PHP 5.3

 

Im looking for a solution to a problem. A site im building stores documents. You can then upload a newer version of that document that overrides the old document. But if you viewed the first document when trying to view the new document it loads the old one up unless you clear your internet files inbetween.

 

Is there a way to make the site not download the file to the clients computer or a way to delete the clients local temp version before downloading and opening the new document?

Link to comment
Share on other sites

Using the header function, you can tell the browser not to cache it, for some information on that: http://lab.artlung.com/anti-cache/

 

The code to do it in PHP:

 

<?php
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');
?>

 

Not sure why the above site posted that with short_open tags assumed to be on, I would use the version I just pasted here over that as an fyi.

Link to comment
Share on other sites

Now I have done just this but its not working, but i bet that is my code which is at fault as I didn't think my method would work as it basically loaded a new page without the header info.

 

My first way was:

<?php
// stop cashing
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>
<a href="file.doc" target="_blank">Click here</a>

 

and my second was in it basic form

<?php
// stop cashing
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
header("Location: file.doc");
?>

Link to comment
Share on other sites

The issue is going to lie within the file.doc. Instead of doing a location redirect, why not just use headers and readfile to deliver the file from the php page? That way the headers work, the file is delivered without being cached and it is a win / win.

 

The other option, if you are using apache with mod_header is to set the .doc extension to not be cached, which would also prevent it from being cached.

Link to comment
Share on other sites

it works perfect now.

 

If it helps anyone this is my code

$file = urldecode($_GET['file']);
if(file_exists($file)) {
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file));
header("Cache-Control: no-cache");
header("Pragma: no-cache");
ob_clean();
flush();
readfile($file);
header("Location: ".urldecode($_GET['return']));
} else {
header("Location: ".urldecode($_GET['return'])."&status=fileErr");
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.