简体   繁体   English

PHP强制下载可在localhost上运行,但不能在托管服务器上运行

[英]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šX v/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šX v/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.

相关问题 强制文件下载代码在本地主机上工作,但在 php 中的实际服务器上不工作 - force file download code work on localhost but not working on actual server in php 使用Wordpress的PHP文件下载可在localhost上运行,但不能在实时服务器上运行 - PHP file download with Wordpress works on localhost but not live server 网站在localhost上运行良好,但在托管服务器上却无法运行 - Website works good in localhost but not in hosting server javascript在localhost上有效,但在托管服务器上失败 - javascript works on localhost but fails on hosting server Laravel 5.6 强制下载在 linux 服务器中不起作用,但在 localhost 中起作用 - Laravel 5.6 force download didn't work in linux server but works in localhost 图片下载在 Xampp 中有效,但在托管服务器中无效 - Image download works in Xampp but not in Hosting server PHP脚本适用于localhost,但不适用于托管 - PHP script works on localhost, but doesn't works on hosting 强制使用PHP下载-服务器配置? - Force Download with PHP - Server Configuration? PHP:Readfile在本地主机上有效,但在服务器上不可用 - PHP: Readfile works at localhost but not at server 这个 php 代码在本地主机上工作,但不在服务器上? - this php code works in localhost but not on server?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM