利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码...

利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码

 分类:
 
mysql(8)   php 算法(20)   php(38) 

  利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码。做了很久终于知道了很好的解决方案。

  1.加载辅助函数

 

[php]  view plain  copy
 
  1. $this->load->helper('download');  //下载辅助函数  
  2. $this->load->helper('string');    //字符编码转换辅助翻书  
 

 

 2.force_download($filename, $data)通过它的代码可以知道$data 必须是字符串,如果不是字符串会出错的。是数组,必须转换成字符串,用函数implode即可,该函数用法见官网,更具我的项目代码如下。

 

[php]  view plain  copy
 
  1. public function download() {  
  2.           // 输出Excel文件头  
  3.          //解决IE,firework等浏览器文件名乱码问题  
  4.         $filename = '已拆回款数据.csv';  
  5.         $newarray=array();该结果是一个二维数组  
  6.         if ($this->input->get () !== false) {  
  7.             $conn = $this->getformdata ();  
  8.         }  
  9.         // 输出Excel列名信息  
  10.         $head = array (  
  11.                 'ID',  
  12.                 '回款号',  
  13.                 '回款金额(分)',  
  14.                 '调整渠道成本',  
  15.                 '回款时间',  
  16.                 '导入SO时间',  
  17.                 '渠道名称',  
  18.                 '合同ID'   
  19.         );  
  20.         foreach ( $head as $i => $v ) {  
  21.             // CSV的Excel支持GBK编码,一定要转换,否则乱码  
  22.              $head [$i] = utf2gbk($v);  
  23.         }  
  24.         $newarray[]=implode(',',$head);  
  25.         $sql = "select * from sinapay_boss_income where {$conn} order by income_status asc, boss_income_id desc";  
  26.         $query = $this->db->query ( $sql );  
  27.         // 计数器  
  28.         $cnt = 0;  
  29.         // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小  
  30.         $limit = 8000;  
  31.         foreach ( $query->result_array () as $row ) {  
  32.             $cnt ++;  
  33.             if ($limit == $cnt) { // 刷新一下输出buffer,防止由于数据过多造成问题  
  34.                 ob_flush ();  
  35.                 flush ();  
  36.                 $cnt = 0;  
  37.             }  
  38.             // 读取表数据  
  39.                 $content = array ();  
  40.                 $content [] =$row ['boss_income_id'];  
  41.                 $content [] =$row ['income_num'];  
  42.                 $content [] =$row ['income_amount'];  
  43.                 $content [] =$row ['modified_cost'];  
  44.                 $content [] =$row ['income_date'];  
  45.                 $content [] =$row ['write_so_month'];  
  46.                 $content [] =utf2gbk($row ['channel_name']);  
  47.                 $content [] =utf2gbk($row ['income_contract_id']);  
  48.             $newarray[]=implode(',',$content);  
  49.         }  
  50.         $newarray=implode("\n",$newarray);  
  51.         force_download($filename, $newarray);   
  52.     }  
  53. }   

 

3.测试后我发现ie浏览器文件名是乱码,firefox 浏览器文件名也是乱码。于是对辅助函数做了以下的修改,以下加了背景颜色的代码是我添加的。

   

[php]  view plain  copy
 
  1. if ( ! function_exists('force_download'))  
  2. {  
  3.     function force_download($filename = '', $data = '')  
  4.     {  
  5.         if ($filename == '' OR $data == '')  
  6.         {  
  7.             return FALSE;  
  8.         }  
  9.         //ie 浏览器解决文件名是乱码  
  10.         $filename = urlencode($filename);  
  11.                $filename = str_replace("+", "%20", $filename);  
  12.         // Try to determine if the filename includes a file extension.  
  13.         // We need it in order to set the MIME type  
  14.         if (FALSE === strpos($filename, '.'))  
  15.         {  
  16.             return FALSE;  
  17.         }  
  18.       
  19.         // Grab the file extension  
  20.         $x = explode('.', $filename);  
  21.         $extension = end($x);  
  22.   
  23.         // Load the mime types  
  24.         @include(APPPATH.'config/mimes'.EXT);  
  25.       
  26.         // Set a default mime if we can't find it  
  27.         if ( ! isset($mimes[$extension]))  
  28.         {  
  29.             $mime = 'application/octet-stream';  
  30.         }  
  31.         else  
  32.         {  
  33.             $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];  
  34.         }  
  35.       
  36.         // Generate the server headers  
  37.         if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))  
  38.         {  
  39.             header('Content-Type: "'.$mime.'"');  
  40.             header('Content-Disposition: attachment; filename="'.$filename.'"');  
  41.             header('Expires: 0');  
  42.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
  43.             header("Content-Transfer-Encoding: binary");  
  44.             header('Pragma: public');  
  45.             header("Content-Length: ".strlen($data));  
  46.         }elseif(strstr($_SERVER['HTTP_USER_AGENT'], "Firefox")){   //解决firefox 文件名乱码  
  47.             header('Content-Type: "'.$mime.'"');  
  48.             header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');  
  49.             header('Expires: 0');  
  50.             header('Cache-Control: must-revalidate, post-check=0, pre-check=0');  
  51.             header("Content-Transfer-Encoding: binary");  
  52.             header('Pragma: public');  
  53.             header("Content-Length: ".strlen($data));  
  54.         }else  
  55.         {  
  56.             header('Content-Type: "'.$mime.'"');  
  57.             header('Content-Disposition: attachment; filename="'.$filename.'"');  
  58.             header("Content-Transfer-Encoding: binary");  
  59.             header('Expires: 0');  
  60.             header('Pragma: no-cache');  
  61.             header("Content-Length: ".strlen($data));  
  62.         }  
  63.       
  64.         exit($data);  
  65.     }  
  66. }  

转载于:https://www.cnblogs.com/clphp/p/5266454.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值