码迷,mamicode.com
首页 > Web开发 > 详细

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

时间:2014-08-04 20:53:47      阅读:628      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   文件   div   

一、下载任意文件:

     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‘);

 

php 实现文件下载,兼容IE、Firefox、Chrome等浏览器,布布扣,bubuko.com

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

标签:style   blog   http   color   os   io   文件   div   

原文地址:http://www.cnblogs.com/h07061108/p/download_file.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!