使用标题内容在PHP中下载没有对话框的创建的Excel文件

时间:2012-10-23 12:04:14

标签: php header download

任何人都可以知道如何强制下载文件而不显示DialogBox(打开/保存)。下面的代码是我下载创建的excel文件的测试脚本,但是出现了下载文件的对话框。

$filename ="excelreport.xls";
$contents = "testdata1 \t testdata2 \t testdata3 \t \n";
header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;

我想自动下载文件并将其保存在指定目录中,不带对话框

2 个答案:

答案 0 :(得分:2)

您可以强行下载而不是显示它但我认为不可能强制浏览器一般下载它而不提示

// fix for IE catching or PHP bug issue
header("Pragma: public");
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
// browser must download file from server instead of cache

// force download dialog
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");

来源:php.net

答案 1 :(得分:1)

出于安全原因,您无法执行此操作。