简体   繁体   English

php标头内容处置

[英]php header Content-disposition

I have a very basic question illustrated by the code snippet below. 我有一个非常基本的问题,由下面的代码片段说明。 This is the relevant part of a much bigger program in which I wish to download a zip file. 这是一个更大的程序的相关部分,我希望在其中下载一个zip文件。 The code as shown, with the comments in place, produce the expected browser output "Download the file now." 显示的代码(带有注释)可以产生预期的浏览器输出“立即下载文件”。

When I un-comment the code, the zip file is correctly downloaded to my browser! 当我取消注释代码时,该zip文件已正确下载到我的浏览器中!

However, the browser output is not produced. 但是,不会产生浏览器输出。 How do I regain control? 如何重新获得控制权? I would like for the user to then have other options. 我想让用户有其他选择。 (By the way, un-commenting the single "Content-disposition" line is sufficient to cause the loss of control.) (顺便说一句,取消注释单个“ Content-disposition”行就足以导致失去控制。)

I have tried including the code, putting it in a function, many possible combinations of ob_start, ob_end flush, etc., all to no avail. 我试过包括代码,将其放入函数中,ob_start,ob_end刷新等许多可能的组合,但均无济于事。 I am sure I am overlooking something very fundamental and would appreciate some suggestions. 我确信我会忽略一些非常基本的内容,并希望您提出一些建议。

Thanks. 谢谢。

<?php
$sZipFN = 'file.zip';
// header("Content-type: application/zip");
// header("Content-disposition: attachment; filename=$sZipFN");
// readfile($sZipFN);
$sMsg = "Download the file now.";        
?>
<html>
  <body>
    <p> <?php echo $sMsg; ?> </p>
  </body>
</html>

You can't - you have to redirect to the options page and then start the download via javascript redirect ( document.location = 'http://download.url/'; ) 您不能-您必须重定向到选项页面,然后通过javascript重定向( document.location = 'http://download.url/'; )开始下载

This will start the download and leave the user on the options page like you desire. 这将开始下载,并根据需要将用户留在选项页面上。

Don't forget to include a 'Click here if the download fails to start' link somewhere near the top of the page (just in case javascript is disabled) 不要忘了在页面顶部附近添加“如果下载无法开始单击此处”链接(以防禁用javascript)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM