PHP的文件下载代码在屏幕上显示gibbrish而不是打开保存/打开窗口。我从PHP.net网站http://www.php.net//manual/en/function.readfile.php获得此代码,请帮助我该怎么办?
$ file1 ='DataUpload.xls'
if (file_exists($file1)) {
header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename='.basename($file1));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file1));
ob_clean();
flush();
readfile($file1);
exit;
}
屏幕上的输出:
> f2 ɀ \pB a = = G9!8X @ “ 1 Calibri1 Calibri1 Calibri1 Calibri1 Arial1 Calibri1 Calibri1 Calibri1 4 Calibri1 Calibri1 Calibri1 Calibri1,8 Calibri18 Calibri1 8 Calibri1 > Calibri1 4 Calibri1 < Calibri1 ? Calibri1h8 Cambria1 Calibri1 Calibri1 Calibri1h8 Cambria1,8 Calibri18 Calibri1 8 Calibri1 Calibri1 Calibri1 < Calibri1 > Calibri1 ? Calibri1 4 Calibri1 4 Calibri1 Cal ibri1 Calibri1 Calibri1 Calibri1 Calibri1 Calibri1 Calibri“$”#,## 0 _);(“$”#,## 0)!“$”# ,## 0 _); [红色]( “$” #,## 0) “” $ “#,## 0.00 _);(” $ “#,## 0.00)' ”“ $” #,## 0.00 _); [红色](“$”#,## 0.00)7 * 2 _(“$”*#,## 0 _); (“$”*(#,## 0); (“$”*“ - ”); (@ )。))(*#,## 0 _); (*(#,#) #0); (*“ - ”); (@ )?,:(“$”*#,## 0.00 _); (“$”*(#,## 0.00); (“$”*“ - ”?? ); (@ )6 + 1 (*#,## 0.00 _); (*(#,## 0.00); (*“ - ”?? ); (@_) . / ' ' &; ' 1 ' ! # * % / “ � ��� �
+
答案 0 :(得分:0)
$file1 = 'DataUpload.xls';
header('Pragma: public'); // required
header('Expires: 0'); // no cache
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Cache-Control: private',false);
header('Content-Type:application/force-download');
header('Content-Disposition: attachment; filename="'.basename($file1).'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.filesize($file1)); // provide file size
header('Connection: close');
readfile($file1);
exit();