简体   繁体   English

PHP标题:内容处理:附件,然后位置:一些网址

[英]PHP Header : Content-disposition: attachment , then Location: some url

My goal is to redirect browser to a new page after browser issues the attachment download. 我的目标是在浏览器发出附件下载后将浏览器重定向到新页面。

header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header("Content-disposition: attachment;filename=file.csv");
// csv output logic here

// forward to next page
header("Location: $url");

Is it possible ? 可能吗 ?

UPDATE : the result of above 更新 :上述结果

The last line of CSV shows PHP error : CSV的最后一行显示PHP错误:

Cannot modify header information - headers already sent by... 无法修改标头信息 - 已由...发送的标头

It is because CSV logic part has modified the contents before the line header("Location: $url"); 这是因为CSV逻辑部分修改了行header("Location: $url");之前的内容header("Location: $url");

UPDATE : I tried an alternative method: to echo a small HTML consist of a key line <meta http-equiv="refresh" content="0; url=$url" /> ( of course <html> <head> <body> and even DOCTYPE are also echoed ). 更新 :我尝试了另一种方法:回显一个由关键行组成的小HTML <meta http-equiv="refresh" content="0; url=$url" /> (当然<html> <head> <body>甚至DOCTYPE也得到了回应)。 But still, the HTML codes are shown in the CSV content. 但是,HTML代码仍显示在CSV内容中。

With the current code, no you can't. 使用当前代码,没有你不能。 You force the browser to download the further content to file.csv so newer headers are ignored. 您强制浏览器将更多内容下载到file.csv,以便忽略更新的标头。

However, have a look at meta refresh stuff. 但是,看看元刷新的东西。 You will be able to add a delay between the page load (so user actually sees a web page) and send the file to download in the refresh. 您将能够在页面加载之间添加延迟(以便用户实际看到网页)并在刷新时将文件发送到下载。

I wish I could add an example but I'm currently in a train surrounded by some babies crying and I'm bored. 我希望我可以添加一个例子,但我现在正坐在一辆被一些婴儿哭泣的火车里,我很无聊。

To prevent the meta tags (and other HTML) appearing in the CSV file content, we can use some $_GET value. 为防止元标记(和其他HTML)出现在CSV文件内容中,我们可以使用一些$ _GET值。

<?php
if (isset($_GET['dl']) && $_GET['dl'] == '1') {
header("Content-type: text/csv");
header("Cache-Control: no-store, no-cache");
header("Content-disposition: attachment;filename=file.csv");
// csv output logic here
}
else{
?>
web page here, with html, head, title, body, and whatnot tags. 
In the head tag, add this:
<meta http-equiv="refresh" content="5;url=http://my.web.site.com/download.php?dl=1"> 
<?php
}
?>

您可以在客户端执行此操作:在新窗口中打开文件下载,然后使用javascript重定向当前窗口。

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

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