laPistola Posted July 14, 2011 Share Posted July 14, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/241993-temp-files/ Share on other sites More sharing options...
premiso Posted July 14, 2011 Share Posted July 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241993-temp-files/#findComment-1242733 Share on other sites More sharing options...
laPistola Posted July 14, 2011 Author Share Posted July 14, 2011 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"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/241993-temp-files/#findComment-1242739 Share on other sites More sharing options...
premiso Posted July 14, 2011 Share Posted July 14, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/241993-temp-files/#findComment-1242742 Share on other sites More sharing options...
laPistola Posted July 14, 2011 Author Share Posted July 14, 2011 Thank you, Thank you and Thank you The mod_header wouldn't be ideal as the file could be one of about 30 different formats. Ill use the readfile() method, looking at the php manual its the perfect solution. Did I thank you Quote Link to comment https://forums.phpfreaks.com/topic/241993-temp-files/#findComment-1242744 Share on other sites More sharing options...
laPistola Posted July 14, 2011 Author Share Posted July 14, 2011 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"); } Quote Link to comment https://forums.phpfreaks.com/topic/241993-temp-files/#findComment-1242758 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.