rfeio Posted October 14, 2009 Share Posted October 14, 2009 Hi, I'm trying to implement a script where when a user clicks a link he gets a PDF file to open or save. In my index.php page I have the following link: <a href="http://localhost/example/get_file.php">Click here to get the file</a> And the script is in get_file.php: <?php $path = '/docs/document.pdf'; $mm_type="application/pdf"; header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Type: " . $mm_type); header("Content-Length: " . filesize($path) ); header('Content-Disposition: attachment; filename="'.basename($path).'"'); header("Content-Transfer-Encoding: binary"); readfile($path); exit(); ?> Unfortunately it's not working properly. When clicking on the link and either selecting 'open' or 'save' from the dialog box, the end result is always the same; the end file is about 1KB in size when originally it was 456KB and therefore it's damaged. Any ideas on why this is happening? Thanks! Link to comment https://forums.phpfreaks.com/topic/177696-solved-help-problem-with-headers-to-download-pdf-file/ Share on other sites More sharing options...
johnsmith153 Posted October 14, 2009 Share Posted October 14, 2009 This works for me for downloading all file types: $path = "/folder/file.pdf"; $file = basename($path); if(file_exists($path)) { header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Transfer-Encoding: binary"); readfile($path); } Link to comment https://forums.phpfreaks.com/topic/177696-solved-help-problem-with-headers-to-download-pdf-file/#findComment-936935 Share on other sites More sharing options...
johnsmith153 Posted October 14, 2009 Share Posted October 14, 2009 I suppose your: header('Content-Disposition: attachment; filename="'.basename($path).'"'); could be the problem Link to comment https://forums.phpfreaks.com/topic/177696-solved-help-problem-with-headers-to-download-pdf-file/#findComment-936950 Share on other sites More sharing options...
dreamwest Posted October 14, 2009 Share Posted October 14, 2009 Add this to htaccess - its foolproof AddType application/octet-stream .pdf Link to comment https://forums.phpfreaks.com/topic/177696-solved-help-problem-with-headers-to-download-pdf-file/#findComment-936955 Share on other sites More sharing options...
rfeio Posted October 14, 2009 Author Share Posted October 14, 2009 Mates, thanks a lot for your help!!! I have found the problem thanks to you johnsmith153. I have tried your code and I realized that the path was wrong! The "file_exists($path)" code warned me of the problem. I had to include in the path the "http://localhost" reference in order to read the file. Link to comment https://forums.phpfreaks.com/topic/177696-solved-help-problem-with-headers-to-download-pdf-file/#findComment-936956 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.