首页 > 技术文章 > php下载文件

gyfluck 2018-01-20 14:53 原文



$arr = array(1,2,3,4);
$file_data = json_encode($res);
$download_filename = '123.json';
$http_user_agent = $_SERVER['HTTP_USER_AGENT'];
header("Content-Type: application/octet-stream");
if (preg_match("/MSIE/", $http_user_agent) ) {
header('Content-Disposition: attachment; filename="'.$download_filename.'"');
} elseif (preg_match("/Firefox/", $http_user_agent)) {
header ('Content-Disposition: attachment; filename*="utf8' . $download_filename . '"' );
} else {
header('Content-Disposition: attachment; filename="'.$download_filename.'"');
}
header("Accept-Length: ".strlen($file_data));
echo $file_data;

试了试,谷歌浏览器、火狐浏览器可直接下载;
IE浏览器如果出现如下弹窗:

解决方案:在ie菜单栏的【页面】-->【编码】,选择【自动选择】然后下载试试。

360浏览器也出现这种问题,待测。



推荐阅读