加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2020-12-13 17:14:40 所属栏目:PHP教程 来源:网络整理
导读:我的目标是在浏览器发出附件下载后将浏览器重定向到新页面. 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("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");

可能吗 ?

更新:上述结果

CSV的最后一行显示PHP错误:

Cannot modify header information – headers already sent by…

这是因为CSV逻辑部分修改了行标题之前的内容(“Location:$url”);

更新:我尝试了另一种方法:回显一个由关键行组成的小HTML< meta http-equiv =“refresh”content =“0; url = $url”/> (当然< html>< head>< body>甚至DOCTYPE也被回应).但是,HTML代码仍显示在CSV内容中.

解决方法

使用当前代码,没有你不能.
您强制浏览器将更多内容下载到file.csv,以便忽略更新的标头.

但是,看看元刷新的东西.您将能够在页面加载之间添加延迟(以便用户实际看到网页)并在刷新时将文件发送到下载.

我希望我可以添加一个例子,但我现在正坐在一辆被一些婴儿哭泣的火车里,我很无聊.

为防止元标记(和其他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
}
?>

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读