在修复与 CSV 相关的下载问题时,我添加了一些 PHP 中常用的标头。之后,添加我执行 CSV 导出测试的那些。从那时起,在我的浏览器中,一个新选项卡将无限打开和关闭。我无法通过还原我所做的更新来阻止它。清除服务器和浏览器 - 缓存和历史记录。尽管如此,在尝试使用旧代码导出时,这些选项卡仍在重新打开。我现在通过sudo pkill -9 chrome
在终端上执行来停止浏览器(Chrome)。
我认为新标签的打开和关闭与我添加的强制下载代码有关。
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');
header('Content-Disposition: attachment; filename='.$filename);
header('Content-Type: application/csv;');
header('Content-Type: application/force-download');
header('Content-Type: application/octet-stream', false);
header('Content-Type: application/download', false);
// file creation
$file = fopen('php://output', 'w');
$header = array("Group Name","Contact Name","Email","Phone Number","Company Name");
fputcsv($file, $header);
foreach ($new_array as $key => $line){
fputcsv($file,$line);
}
fclose($file);
exit;
$new_array
是从 DB 中获取的数据数组。
我认为问题是因为我忘记添加缓存标头,例如:
header('Cache-Control: public, must-revalidate, max-age=0');
header('Pragma: public');
header('Expires: Sat, 26 Jul 1997 05:00:00 GMT');
header('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
有没有办法阻止这个问题?
查看问题的视频链接