我有一个使用header content-type
生成excel报告的页面,并将生成信息保存在数据库中(例如,生成报告的人员,等等)我的问题是我无法重定向页面的原因。下面是示例代码/算法
// excel content
/* excel content populates here */
// output to excel file
header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$positionFileName);
header("Cache-Control: max-age=0");
// save logs on the database
/* save logs code executed here */
// redirect page to get the logs on the database that display on the web page
header("location: report.php"); *<--- I can't redirect to report.php*
这正是我想要的
用户将做的是选择报告类型然后单击按钮生成以输出excel报告(打开/保存对话框)。然后,系统将在数据库中保存生成的报告的日志,然后在页面上显示。这就是为什么我想在report.php上再次重定向它,以便日志将再次出现在数据库上以显示在浏览器上。
答案 0 :(得分:2)
标题发生冲突。首先你说“这里来了一个文件”,使用Content-Disposition: attachment
,然后你说“哦不,它不会来,对XYZ做一个额外的请求” ,使用Location
标题。您可以省略Content-
标题以获得相同的结果。
这对我有用:
header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=foo.dat");
header("location: foo.dat");
因为我被重定向到foo.dat文件,该文件显示在浏览器中(因此忽略了Content
- 标题,因为在Location
- 标题之后发出了一个新请求, 不在其响应中获取Content
- 标头。如果您要对某些扩展或目录强制attachment
,configure your web server to emit those headers,或使用{{1没有fpassthru($filename)
- 标头直接发出文件而不会遇到内存错误:
Location
但请注意,在前三个header("Content-Type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$positionFileName);
header("Cache-Control: max-age=0");
// save logs on the database
/* save logs code executed here */
fpasstrhu($positionFileName);
语句之后执行代码期间生成的任何输出都将输出到浏览器,这将认为它是文件数据。
我猜这当前也在发生,导致header()
失败。 Enable error reporting to see why and where there was data printed
答案 1 :(得分:1)
它不起作用,因为你告诉浏览器它应该期望一个Excel文件。那么,在您告诉浏览器它应该有一个Excel文件后,您正试图重定向到另一个页面。问题出在你的方法上。
答案 2 :(得分:-1)
检查您是否尚未向浏览器输出任何内容。
'location'通常也应该是'Location'。
答案 3 :(得分:-1)
编辑本 header(“Location:report.php”);
答案 4 :(得分:-1)
header( "Location: http://" . strip_tags( $_SERVER ['HTTP_HOST'] ) . "/report.php" );
试试这个..我总是调用HTTP主机检查程序,以确保我保持在同一个域。
编辑:我的不好,我在用大写字母L写的时候就知道了...对不起早期的说明。