ThaSaltyDawg Posted July 21, 2010 Share Posted July 21, 2010 I have code (attached) that I used to download pdf files outside the root directory. This is done because users are required to login. I am having to transfer the site to a new server and I have everything working right except for this part. When a link is clicked to download a file I get the following error in adode acrobat: Acrobat could not open 'file.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded). When trying to down an msword .doc file I get the scribble text characters. My previous server was FreeBSD and my new server is Red Hat. I'm not sure if there are any setting I must change. $dl_path = '/home/ebtronweb/www/download/'; $filename = 'timelog.txt'; $filename = $dl_path.$filename; // 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 "txt": $ctype="text/plain"; break; 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(); is there code with an alternative way to download a file like this? Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/ Share on other sites More sharing options...
simshaun Posted July 21, 2010 Share Posted July 21, 2010 Try commenting out the headers and make sure readfile() is giving you the correct content, and not an error. Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/#findComment-1089207 Share on other sites More sharing options...
ThaSaltyDawg Posted July 21, 2010 Author Share Posted July 21, 2010 I get: %PDF-1.6 %âãÏÓ 32 0 obj <> endobj xref 32 52 0000000016 00000 n 0000001733 00000 n 0000001869 00000 n 0000002039 00000 n 0000002080 00000 n 0000002121 00000 n 0000002300 00000 n 0000002517 00000 n 0000002947 00000 n 0000003636 00000 n 0000003718 00000 n 0000004438 00000 n 0000005132 00000 n 0000005772 00000 n 0000006460 00000 n 0000007152 00000 n 0000007509 00000 n 0000007894 00000 n 0000027410 00000 n 0000027654 00000 n 0000043383 00000 n 0000043642 00000 n 0000044333 00000 n 0000044520 00000 n 0000044848 00000 n 0000045392 00000 n 0000045734 00000 n 0000046309 00000 n 0000046655 00000 n 0000046945 00000 n 0000047272 00000 n 0000066340 00000 n 0000066619 00000 n 0000078131 00000 n 0000078415 00000 n 0000088916 00000 n 0000089188 00000 n 0000089493 00000 n 0000089698 00000 n 0000089990 00000 n 0000090057 00000 n 0000090867 00000 n 0000091586 00000 n 0000226721 00000 n 0000243402 00000 n 0000243664 00000 n 0000260902 00000 n 0000261152 00000 n 0000275093 00000 n 0000275350 00000 n 0000284150 00000 n 0000001361 00000 n trailer <<7D6E107A9AE0BF45B51712ECF7A2EE89>]>> startxref 0 %%EOF 83 0 obj<>stream –Mas)˜hÄEõÄ X6•&mtEÍâh Aåe€´Î–1“9©‰mzÀAF®9‰r v™Ê³×LšÎ‡icÅÈrçQè̬œc4sQW–ÿ}QìûcÄ–LÕeÃÔÆ-x.âTÈ^.kN®06ÚßݧͬR¬WFúhUá‚G™ÛÎ= Ò¶SkPj*‰ ÓxAÖçS¹RñØtËݽJK+ï]s‰f:A[ôúÿ ¹šv7ZZ¿.‰U&x_â窰)˜U2Z`"5Á»"°9V-Ї ’[<:¦KéÖ–¤ ...there is more ofcourse... I I have had this before but cannot remember how I fixed it. Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/#findComment-1089223 Share on other sites More sharing options...
Daniel0 Posted July 21, 2010 Share Posted July 21, 2010 Are you sure that your Content-Type header is sent correctly? Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/#findComment-1089225 Share on other sites More sharing options...
ThaSaltyDawg Posted July 21, 2010 Author Share Posted July 21, 2010 For pdf files I use header('Content-Type: application/pdf'); and for doc files I use header('Content-Type: application/doc'); I have other code I have tried to, but none work. I am sure that I am missing something here but I can't find it. $dl_path = '/home/ebtronweb/www/media/pdf/'; $filename = '2009_BAG_Brochure3.pdf'; $file = $dl_path.$filename; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: application/pdf'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/download"); header("Content-Disposition: attachment;filename=$filename "); header("Content-Transfer-Encoding: binary "); header('Content-Length: ' . filesize($file)); ob_clean(); flush(); readfile($file); exit; } else { echo "<div style=\"font-size:44px; color:#ff0000; font-weight:900\"><br>File Does Not Exist</div><br>$filename"; } Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/#findComment-1089237 Share on other sites More sharing options...
simshaun Posted July 21, 2010 Share Posted July 21, 2010 Is there any type of compression enabled on your server? Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/#findComment-1089241 Share on other sites More sharing options...
ThaSaltyDawg Posted July 22, 2010 Author Share Posted July 22, 2010 How do I check for compression enabled on the server? Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/#findComment-1089578 Share on other sites More sharing options...
ThaSaltyDawg Posted July 22, 2010 Author Share Posted July 22, 2010 Somehow I got it work. I was using a link like: $tUrl= "?page=downloads&filetype=pdf&filename=$tFilename"; and sending it to a page with the following attached code. // if(isset($_REQUEST['filename'])) { $FileName = $_REQUEST['filename']; } else { $FileName = NULL; } // if(isset($_REQUEST['filetype'])) { $FileType = $_REQUEST['filetype']; } else { $FileType = NULL; } switch(strtoupper($FileType)) { case "PDF": $dl_path = '/home/web/www/media/downloads/pdf/'; $ctype = "application/pdf"; break; case "XLS": $dl_path = '/home/web/www/media/downloads/xls/'; $ctype = "application/vnd.ms-excel"; break; case "DOC": $dl_path = '/home/web/www/media/downloads/doc/'; $ctype = "application/msword"; break; } $file = $dl_path.$FileName; if (file_exists($file)) { header('Content-Description: File Transfer'); header('Content-Type: $ctype'); header("Pragma: public"); header("Expires: 0"); header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); header("Content-Type: application/force-download"); header("Content-Type: application/download"); header("Content-Disposition: attachment;filename=$FileName "); header("Content-Transfer-Encoding: binary "); header('Content-Length: ' . filesize($file)); //ob_clean(); //flush(); readfile($file); exit; } else { echo "<div style=\"font-size:44px; color:#ff0000; font-weight:900\"><br>File Does Not Exist</div><br>$FileName"; } But I kept getting those errors. All I did was place that file on the root directory and link directly to it and it work. $tUrl= "download.php?filetype=pdf&filename=$tFilename"; I checked to see if there was a problem with my paths or something but it was not. However just by placing it on the root it work. While I got it work, I still do not understand what happened? But thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/208434-download-outside-root-with-header/#findComment-1089794 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.