php 程序实现文件下载,兼容IE、Firefox、Chrome等浏览器

//这里是下载zip文件
header("Content-Type: application/zip");
header("Content-type: application/octet-stream");
header("Content-Transfer-Encoding: Binary");
header("Content-Length: " . filesize($file_path));
$ua = $_SERVER [ 'HTTP_USER_AGENT' ];
$file_name = basename($file_path);
$encoded_filename = urlencode($file_name);
$encoded_filename = str_replace("+", "%20", $encoded_filename);
if (preg_match("/Chrome/", $ua))
{
    header('Content-disposition: attachment; filename=' . $file_name);
}else if (preg_match("/Firefox/", $ua)) {
    header("Content-Disposition: attachment; filename=\"" . $file_name."\"");//火狐要用双引号
}
else{
    header('Content-Disposition: attachment; filename="' . rawurlencode($file_name) . '"');
}
header("Content-Transfer-Encoding: binary"); //告诉浏览器,这是二进制文件
header('Content-Length: ' . filesize($file_path));
@readfile($file_path);
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章