playaz Posted June 26, 2007 Share Posted June 26, 2007 Hi guys, I have a small script that does a 'force download' of a pdf file - the problem is while it downloads the correct file, when I attempt to open the file once download it is corrupted - I get the following error in Adobe Acrobat: "There was an error opening this document. The file is damaged and could not be repaired." if ($_POST && $_REQUEST['document_id'] != '') { $oDocuments = new Documentation; $arrDocument = $oDocuments->getFrontItem($_REQUEST['document_id']); // Define the path to file $file = $arrDocument['filename']; // eg 'application.pdf' if(!file) { // File doesn't exist, output error die('file not found'); } else { // Set headers header("Cache-Control: public"); header("Content-Description: File Transfer"); header("Content-Disposition: attachment; filename=$file"); header("Content-Type: application/pdf"); header("Content-Transfer-Encoding: binary"); // Read the file from disk //readfile($file); if($fp = fopen($path.$file, "r")) { while(!feof($fp)) { echo fgets($fp, 4096); } // close the file fclose($fp); } } } Link to comment https://forums.phpfreaks.com/topic/57239-help-with-file-download/ Share on other sites More sharing options...
redarrow Posted June 26, 2007 Share Posted June 26, 2007 >>>>>>>>>>>>>>>>> case "pdf": $ctype="application/pdf"; break; <<<<<<<<<<<<<<<< <?php $filename = $_GET['file']; // required for IE, otherwise Content-disposition is ignored if(ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // addition by Jorg Weske $file_extension = strtolower(substr(strrchr($filename,"."),1)); if( $filename == "" ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: download file NOT SPECIFIED. USE force-download.php?file=filepath</body></html>"; exit; } elseif ( ! file_exists( $filename ) ) { echo "<html><title>eLouai's Download Script</title><body>ERROR: File not found. USE force-download.php?file=filepath</body></html>"; exit; }; switch( $file_extension ) { case "pdf": $ctype="application/pdf"; break; case "exe": $ctype="application/octet-stream"; break; case "zip": $ctype="application/zip"; break; case "doc": $ctype="application/msword"; break; case "xls": $ctype="application/vnd.ms-excel"; break; case "ppt": $ctype="application/vnd.ms-powerpoint"; break; case "gif": $ctype="image/gif"; break; case "png": $ctype="image/png"; break; case "jpeg": case "jpg": $ctype="image/jpg"; break; default: $ctype="application/force-download"; } header("Pragma: public"); // required header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Cache-Control: private",false); // required for certain browsers header("Content-Type: $ctype"); // change, added quotes to allow spaces in filenames, by Rajkumar Singh header("Content-Disposition: attachment; filename=\"".basename($filename)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($filename)); readfile("$filename"); exit(); ?> Link to comment https://forums.phpfreaks.com/topic/57239-help-with-file-download/#findComment-282926 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.