[英]PHP force download works on localhost but not on hosting server
I am trying to force download a file using following code 我正在尝试使用以下代码强制下载文件
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='backup.zip');
header('Content-Length: '.filesize('backup.zip') );
readfile('backup.zip');
This works totally fine on localhost (xampp) but when I uploaded to my hosting server, it just prints some weird characters on the screen like below. 这在localhost(xampp)上完全可以正常工作,但是当我上载到托管服务器时,它只在屏幕上显示一些奇怪的字符,如下所示。
PK|^EWÌV†Ÿcredit_return.csvUT ”:QT”:QTux???eK?ƒ0 ÷>‹+Ù&ßÓ ÔdÁ¢P¸“ÐB(Ê"Ñdôì÷L1ŒkŸâº¥©¾cZæi)Ïm‰©ÜÃkÞ¦Ÿ?~å0¬ r>¡?«Ù°(…Ð說[Ó¯Pt«:
B]ñE3ºKª¯ª
÷G…»êJKs‚B³ÓsšXv/qÊ™T%ØIƒ=°½mÑ°Cß–cÊÀ~ßM”r—ÒJ_hɼÑ?PK|^EÑàÀUäv?txn_type.csvUT ”:QT”:QTux???]'KOB1?…÷üŠ¬jBñYÂÂ'¸$Mï½3dnAø÷V!qŽ»v¾³8râu9ïiZW®o?ý~6Y¾Ü¡'tàÝpvª°§æ¦Y?“†…¤Ê'¾bD§®Ûл* MŸú/Ï?ðñöb¾¶æ?¼o0P'ºÁ_K³‡‹äÒ« ;kayRÂbUÔ»Ëu–F5‚^?‚Bóê?½œÿB4-pÄÀ¢'¡C
çëññ/PK|^E ~ìa3; user.csvUT ”:QT”:QTux???+-N-ŠÏLÑÉKÌMÕ)rÀŒ‚Äââòü¢.CÇ”òÄÌD(eibdlhbbdbÄPK|^EeHÐ'åû?definitions.sqlUT ”:QT”:QTux???¥ÕOo‚0ð»É PK | ^EWÌV†Ÿcredit_return.csvUT“:QT”:QTux ??? eK?ƒ0÷> ‹+Ù&ßÓÁdÁ¢P¸“ÐB(Ê”Ñdôì÷L1ŒkŸâ¥©¾cZæi)Ïm‰©ÜÃkæŸ?〜å0 ¬r>¡?«Ù°(…Ð說[Ó¯Pt«:B]ñE3ºKª¯ª
÷G…»êJKs‚B³ÓsšXv/qÊ™T%ØIƒ=°½mÑ°Cß–cÊÀ~ßM”r—ÒJ_hɼÑ?PK|^EÑàÀUäv?txn_type.csvUT ”:QT”:QTux???]'KOB1?…÷üŠ¬jBñYÂÂ'¸$Mï½3dnAø÷V!qŽ»v¾³8râu9ïiZW®o?ý~6Y¾Ü¡'tàÝpvª°§æ¦Y?“†…¤Ê'¾bD§®Ûл* MŸú/Ï?ðñöb¾¶æ?¼o0P'ºÁ_K³‡‹äÒ« ;kayRÂbUÔ»Ëu–F5‚^?‚Bóê?½œÿB4-pÄÀ¢'¡C
CCçëññ/ PK | ^ E〜ìa3; user.csvUT“:QT”:QTux ??? +-N-ŠÏLÑÉKÌMÕ)rÀŒÄââü¢.CÇ“òÄÌD(eibdlhbbdbÄPK| ^EeHÐ'åû?definitions.sqlUT”:QT“:QTux? ¥¥ Oo‚0ð»É
I did lot of search on google, stackoverflow itself has many questions regarding this but most are unanswered or not satisfacorily answered. 我在Google上进行了很多搜索,stackoverflow本身对此有很多问题,但大多数都没有得到答案或没有得到令人满意的回答。
Hint: This may be related to output buffer. 提示:这可能与输出缓冲区有关。
The weird characters start with "PK", so i think you are looking at the contents of backup.zip (remember PKZIP). 怪异的字符以“ PK”开头,所以我认为您正在查看backup.zip的内容(请记住PKZIP)。 Your browser does not seem to know what to do with it. 您的浏览器似乎不知道如何处理。
There is an example provided by php: http://php.net/manual/en/function.readfile.php which has more header-stuff. php提供了一个示例: http : //php.net/manual/en/function.readfile.php ,其中包含更多的标头信息。 might do the trick. 可能会解决问题。
The header that declares it as an attachment does not have the quotes right in your example. 将其声明为附件的标头在您的示例中没有引号。 You could use double quotes around to filename and an extra single quote after the filename to make the quoting "correct". 您可以在文件名前后使用双引号,并在文件名后使用额外的单引号,以使引号“正确”。
header('Content-Disposition: attachment; filename="backup.zip"');
Because of the incorrect quotes, the entire header may not be in effect. 由于不正确的引号,整个标头可能无效。
Try the following. 请尝试以下方法。
header('Content-Type: application/octet-stream');
header("Content-Disposition: attachment; filename='backup.zip'");
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
ob_clean();
flush();
readfile('backup.zip');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.