【发布时间】:2019-03-20 17:17:00
【问题描述】:
我有一个下载 zip 文件的 php 页面。通常在下载后,它会使用header('location:myfiles.php');自动将用户返回到上一页(myfiles.php)。
当我执行该页面时,它会将我带到myfiles.php,但不会出现下载弹出窗口,从而阻止我下载我的 zip 文件。当我删除header('location:myfiles.php'); 行时,我可以按预期下载我的 zip 文件。
以下是我的代码摘录。
//Some codes
if(file_exists($zip_name)){
// push to download the zip
header('Content-type: application/zip');
header('Content-Disposition: attachment; filename="'.$zip_name.'"');
readfile($zip_name);
// remove zip file is exists in temp path
unlink($zip_name);
}
session_start();
$_SESSION['correct']="Files downloaded sucessfully";
header('location:myfiles.php');
您能帮我找到解决方法吗?谢谢。
【问题讨论】:
-
readfile输出数据,内容发送后不能使用header。session_start也是如此,它应该在任何输出之前调用。 -
根据浏览器请求,您可以发回页面或要求它加载不同的位置;不是两者。
-
我认为您在下载后无法使用标头重定向。下载过程是一个特定的过程,它必须是某个页面。此外,所有响应(带有标题)都在您的项目中一次出现。所以你不能用 readfile 做到这一点。如果您使用发送二进制文件并使用 ob_flush() 函数“可能”,则“可能”是可能的。
-
感谢您的准确回复,我将从我的代码中删除
session部分。
标签: php http-headers