2014-07-31 12:53 1047人阅读 评论(0) 收藏 举报
 分类:
mysql(8)  php 算法(20)  php(38) 

版权声明:本文为博主原创文章,未经博主允许不得转载。

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

1.加载辅助函数

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

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

  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 浏览器文件名也是乱码。于是对辅助函数做了以下的修改,以下加了背景颜色的代码是我添加的。

  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. // Grab the file extension
  19. $x = explode('.', $filename);
  20. $extension = end($x);
  21. // Load the mime types
  22. @include(APPPATH.'config/mimes'.EXT);
  23. // Set a default mime if we can't find it
  24. if ( ! isset($mimes[$extension]))
  25. {
  26. $mime = 'application/octet-stream';
  27. }
  28. else
  29. {
  30. $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
  31. }
  32. // Generate the server headers
  33. if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
  34. {
  35. header('Content-Type: "'.$mime.'"');
  36. header('Content-Disposition: attachment; filename="'.$filename.'"');
  37. header('Expires: 0');
  38. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  39. header("Content-Transfer-Encoding: binary");
  40. header('Pragma: public');
  41. header("Content-Length: ".strlen($data));
  42. }elseif(strstr($_SERVER['HTTP_USER_AGENT'], "Firefox")){   //解决firefox 文件名乱码
  43. header('Content-Type: "'.$mime.'"');
  44. header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
  45. header('Expires: 0');
  46. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  47. header("Content-Transfer-Encoding: binary");
  48. header('Pragma: public');
  49. header("Content-Length: ".strlen($data));
  50. }else
  51. {
  52. header('Content-Type: "'.$mime.'"');
  53. header('Content-Disposition: attachment; filename="'.$filename.'"');
  54. header("Content-Transfer-Encoding: binary");
  55. header('Expires: 0');
  56. header('Pragma: no-cache');
  57. header("Content-Length: ".strlen($data));
  58. }
  59. exit($data);
  60. }
  61. }

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

  1. HTTP 下载文件中文文件名在 Firefox 下乱码问题

    转自:http://www.imhdr.com/991/ HTTP 下载文件,中文文件名在 Firefox 下乱码问题 最近帮助一同事解决 HTTP 下载文件时,中文文件名在 Firefox 下乱码的 ...

  2. Struct2 csv文件上传读取中文内容乱码

    网络上搜索下,发现都不适合 最终改写代码: FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br= n ...

  3. 使用JavaScript下载csv文件

    前端可以使用JavaScript在客户端下载包含页面数据的文件,这里以下载CSV格式文件为例,代码如下: function downloadData(data, filename, type) { v ...

  4. HTML save data to CSV or excel

    /********************************************************************************* * HTML save data ...

  5. Linux系统下利用wget命令把整站下载做镜像网站

    Linux系统下利用wget命令把整站下载做镜像网站 2011-05-28 18:13:01 | 1次阅读 | 评论:0 条 | itokit  在linux下完整的用wget命令整站采集网站做镜像 ...

  6. Mac上利用Aria2加速百度网盘下载

    百度网盘下载东西的速度那叫一个慢,特别是大文件,看着所需时间几个小时以上,让人很不舒服,本文记录自己在mac上利用工具Aria2加速的教程,windows下思路也是一样! 科普(可以不看) 这里顺带科 ...

  7. Use JavaScript to Export Your Data as CSV

    原文: http://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/ --------------- ...

  8. [Python] Read and plot data from csv file

    Install: pip install pandas pip install matplotlib # check out the doc from site import pandas as pd ...

  9. js实现使用文件流下载csv文件

    1. 理解Blob对象 在Blob对象出现之前,在javascript中一直没有比较好的方式处理二进制文件,自从有了Blob了,我们就可以使用它操作二进制数据了.现在我们开始来理解下Bolb对象及它的 ...

随机推荐

  1. 从客户端中检测到有潜在危险的 Request.Form 值

    今天在使用Kindeditor的时候,出现了如题的错误. 错误如图: 百度了下,艰难的找了原来是Framework的问题,原来用的2.0,后面变成了4.0,验证级别也更高了: 解决办法:在config ...

  2. HDU 4630 No Pain No Game 线段树 和 hdu3333有共同点

    No Pain No Game Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)T ...

  3. 2016年10月29日 星期六 --出埃及记 Exodus 19:14

    2016年10月29日 星期六 --出埃及记 Exodus 19:14 After Moses had gone down the mountain to the people, he consecr ...

  4. mvcAPI (入门 1)

    步骤: 1)建立order 类 2)建立OrderEntity类 3)创建控制器API 这时候能看到Json 格式的数据啦 5)想在网页或客户端显示 添加一个网页 如下: <!DOCTYPE h ...

  5. 优化Linux生产服务器的经验之谈

    [51CTO独家特稿]如何优化自己的Linux生产服务器?本文结合实际的工作经验,总结了优化Linux生产服务器的九大要点.如果有些方法您尚未采用,不妨一试. 一.时间同步 生产环境下的服务器对时间的 ...

  6. ECharts开始

    为ECharts准备一个具备大小(宽高)的Dom(当然可以是动态生成的) //from echarts example <body> <div id="main" ...

  7. MySQL(二) —— 数据类型与操作数据表

    数据类型 数据类型是指列.存储过程参数.表达式和局部变量的数据特征,它决定了数据的存储格式,代表了不同的信息类型. 整型:TYNINT(-2^7 ~ 2^7-1); SMALLINT(-2^15 ~ ...

  8. [SAP ABAP开发技术总结]动态语句、动态程序

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  9. HDU 5831 Rikka with Parenthesis II(六花与括号II)

    31 Rikka with Parenthesis II (六花与括号II) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  10. 网上搜集的jq常用代码

    1. 设置IE特有的功能:  if ($.browser.msie) { //do something... } 2. 使用jQuery来代替一个元素: $('#thatdiv').replaceWi ...