首页 > 代码库 > php 实现文件下载,兼容IE、Firefox、Chrome等浏览器
php 实现文件下载,兼容IE、Firefox、Chrome等浏览器
一、下载任意文件:
Header("Content-type: application/octet-stream"); $ua = $_SERVER["HTTP_USER_AGENT"]; $encoded_filename = rawurlencode($filename); if (preg_match("/MSIE/", $ua)) { header(‘Content-Disposition: attachment; filename="‘ . $encoded_filename . ‘"‘); } else if (preg_match("/Firefox/", $ua)) { header("Content-Disposition: attachment; filename*=\"utf8‘‘" . $filename . ‘"‘); } else { header(‘Content-Disposition: attachment; filename="‘ . $filename . ‘"‘); } $file = ‘xxxx.pdf‘; header("Content-Length: ". filesize("{$file}"));
二、PHPExcel导出excel文件下载:
...
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, ‘Excel5‘); header(‘Content-Type: application/vnd.ms-excel‘); $ua = $_SERVER["HTTP_USER_AGENT"]; $encoded_filename = rawurlencode($filename); if (preg_match("/MSIE/", $ua)) { header(‘Content-Disposition: attachment; filename="‘ . $encoded_filename . ‘"‘); } else if (preg_match("/Firefox/", $ua)) { header("Content-Disposition: attachment; filename*=\"utf8‘‘" . $filename . ‘"‘); } else { header(‘Content-Disposition: attachment; filename="‘ . $filename . ‘"‘); } header(‘Cache-Control: max-age=0‘); $objWriter->save(‘php://output‘);
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。