spiderwell Posted August 9, 2011 Share Posted August 9, 2011 Hi guys This one has been trouble for me for a qhile now, and I have tried various methods and none seem to work on pdf downloads, the file downloads only 1 byte or there abouts. All other files download ok , just PDFs seem not to. here is my code, the $filecontrol->insertFileDownload() are just database logs being inserted <?php ob_start(); session_start(); include("../classes/files.php"); $filecontrol = new FileControl(); $var_fileid = $_GET['fileid']; $userid = (isset($_SESSION['userid'])) ? $_SESSION['userid'] : 0; if ($userid == 0) die('you are not logged in'); if ($filecontrol->isFilePublic($var_fileid)) { $file = "../filedump/" . $filecontrol->getFileName($var_fileid); $filecontrol->insertFileDownload($var_fileid,$userid,true); } else { if ($filecontrol->doesUserHaveRights($var_fileid)) { $file = "../filedump/" . $filecontrol->getFileName($var_fileid); $filecontrol->insertFileDownload($var_fileid,$userid,true); } else { $file = "../filedump/" . "norights.txt"; $filecontrol->insertFileDownload($var_fileid,$userid,false); } } $file_extension = strtolower(substr(strrchr($file,"."),1)); 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"); header("Content-Disposition: attachment; filename=\"".basename($file)."\";" ); header("Content-Transfer-Encoding: binary"); header("Content-Length: ".filesize($file)); readfile("$file"); exit(); thanks for reading Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/ Share on other sites More sharing options...
spiderwell Posted August 11, 2011 Author Share Posted August 11, 2011 no one got any ideas? Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1255949 Share on other sites More sharing options...
guyfromfl Posted August 11, 2011 Share Posted August 11, 2011 for the $ctype in the switch try MIME not application/pdf.. I have a similar script, and that is what I'm using. Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1255960 Share on other sites More sharing options...
spiderwell Posted August 14, 2011 Author Share Posted August 14, 2011 i will give it a go!! Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1257434 Share on other sites More sharing options...
spiderwell Posted August 14, 2011 Author Share Posted August 14, 2011 it didnt work this is most annoying!! lol Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1257437 Share on other sites More sharing options...
xyph Posted August 14, 2011 Share Posted August 14, 2011 http://elouai.com/force-download.php application/octet-stream will force a download with most browsers from what I remember Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1257463 Share on other sites More sharing options...
spiderwell Posted August 16, 2011 Author Share Posted August 16, 2011 http://elouai.com/force-download.php application/octet-stream will force a download with most browsers from what I remember this was where i took my script from! lol. I will have another try with this. Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1258124 Share on other sites More sharing options...
spiderwell Posted August 16, 2011 Author Share Posted August 16, 2011 mmm that didnt work either, i will i think strip out the stuff above and have another go and work backwards from having just the force download and re add the logging bits to see if that makes any difference Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1258137 Share on other sites More sharing options...
PFMaBiSmAd Posted August 16, 2011 Share Posted August 16, 2011 Have you checked directly if the offending .pdf file is a complete valid file? Perhaps it is only one byte? Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1258140 Share on other sites More sharing options...
xyph Posted August 17, 2011 Share Posted August 17, 2011 Using application/octet-stream in the header forces the download of a PDF on my machine. Keep in mind you're dealing with client behavior here, so you may get different results on different set-ups. Quote Link to comment https://forums.phpfreaks.com/topic/244339-force-download-script-failing-with-pdfs/#findComment-1258342 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.