利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码
php 算法(20)
php(38) 
版权声明:本文为博主原创文章,未经博主允许不得转载。
利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码。做了很久终于知道了很好的解决方案。
1.加载辅助函数
- $this->load->helper('download'); //下载辅助函数
- $this->load->helper('string'); //字符编码转换辅助翻书
2.force_download($filename, $data)通过它的代码可以知道$data 必须是字符串,如果不是字符串会出错的。是数组,必须转换成字符串,用函数implode即可,该函数用法见官网,更具我的项目代码如下。
- public function download() {
- // 输出Excel文件头
- //解决IE,firework等浏览器文件名乱码问题
- $filename = '已拆回款数据.csv';
- $newarray=array();该结果是一个二维数组
- if ($this->input->get () !== false) {
- $conn = $this->getformdata ();
- }
- // 输出Excel列名信息
- $head = array (
- 'ID',
- '回款号',
- '回款金额(分)',
- '调整渠道成本',
- '回款时间',
- '导入SO时间',
- '渠道名称',
- '合同ID'
- );
- foreach ( $head as $i => $v ) {
- // CSV的Excel支持GBK编码,一定要转换,否则乱码
- $head [$i] = utf2gbk($v);
- }
- $newarray[]=implode(',',$head);
- $sql = "select * from sinapay_boss_income where {$conn} order by income_status asc, boss_income_id desc";
- $query = $this->db->query ( $sql );
- // 计数器
- $cnt = 0;
- // 每隔$limit行,刷新一下输出buffer,不要太大,也不要太小
- $limit = 8000;
- foreach ( $query->result_array () as $row ) {
- $cnt ++;
- if ($limit == $cnt) { // 刷新一下输出buffer,防止由于数据过多造成问题
- ob_flush ();
- flush ();
- $cnt = 0;
- }
- // 读取表数据
- $content = array ();
- $content [] =$row ['boss_income_id'];
- $content [] =$row ['income_num'];
- $content [] =$row ['income_amount'];
- $content [] =$row ['modified_cost'];
- $content [] =$row ['income_date'];
- $content [] =$row ['write_so_month'];
- $content [] =utf2gbk($row ['channel_name']);
- $content [] =utf2gbk($row ['income_contract_id']);
- $newarray[]=implode(',',$content);
- }
- $newarray=implode("\n",$newarray);
- force_download($filename, $newarray);
- }
- }
3.测试后我发现ie浏览器文件名是乱码,firefox 浏览器文件名也是乱码。于是对辅助函数做了以下的修改,以下加了背景颜色的代码是我添加的。
- if ( ! function_exists('force_download'))
- {
- function force_download($filename = '', $data = '')
- {
- if ($filename == '' OR $data == '')
- {
- return FALSE;
- }
- //ie 浏览器解决文件名是乱码
- $filename = urlencode($filename);
- $filename = str_replace("+", "%20", $filename);
- // Try to determine if the filename includes a file extension.
- // We need it in order to set the MIME type
- if (FALSE === strpos($filename, '.'))
- {
- return FALSE;
- }
- // Grab the file extension
- $x = explode('.', $filename);
- $extension = end($x);
- // Load the mime types
- @include(APPPATH.'config/mimes'.EXT);
- // Set a default mime if we can't find it
- if ( ! isset($mimes[$extension]))
- {
- $mime = 'application/octet-stream';
- }
- else
- {
- $mime = (is_array($mimes[$extension])) ? $mimes[$extension][0] : $mimes[$extension];
- }
- // Generate the server headers
- if (strstr($_SERVER['HTTP_USER_AGENT'], "MSIE"))
- {
- header('Content-Type: "'.$mime.'"');
- header('Content-Disposition: attachment; filename="'.$filename.'"');
- header('Expires: 0');
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header("Content-Transfer-Encoding: binary");
- header('Pragma: public');
- header("Content-Length: ".strlen($data));
- }elseif(strstr($_SERVER['HTTP_USER_AGENT'], "Firefox")){ //解决firefox 文件名乱码
- header('Content-Type: "'.$mime.'"');
- header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"');
- header('Expires: 0');
- header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
- header("Content-Transfer-Encoding: binary");
- header('Pragma: public');
- header("Content-Length: ".strlen($data));
- }else
- {
- header('Content-Type: "'.$mime.'"');
- header('Content-Disposition: attachment; filename="'.$filename.'"');
- header("Content-Transfer-Encoding: binary");
- header('Expires: 0');
- header('Pragma: no-cache');
- header("Content-Length: ".strlen($data));
- }
- exit($data);
- }
- }
利用php CI force_download($filename, $data) 下载.csv 文件解决文件名乱码,文件内容乱码的更多相关文章
- HTTP 下载文件中文文件名在 Firefox 下乱码问题
转自:http://www.imhdr.com/991/ HTTP 下载文件,中文文件名在 Firefox 下乱码问题 最近帮助一同事解决 HTTP 下载文件时,中文文件名在 Firefox 下乱码的 ...
- Struct2 csv文件上传读取中文内容乱码
网络上搜索下,发现都不适合 最终改写代码: FileInputStream fis = null; InputStreamReader isr = null; BufferedReader br= n ...
- 使用JavaScript下载csv文件
前端可以使用JavaScript在客户端下载包含页面数据的文件,这里以下载CSV格式文件为例,代码如下: function downloadData(data, filename, type) { v ...
- HTML save data to CSV or excel
/********************************************************************************* * HTML save data ...
- Linux系统下利用wget命令把整站下载做镜像网站
Linux系统下利用wget命令把整站下载做镜像网站 2011-05-28 18:13:01 | 1次阅读 | 评论:0 条 | itokit 在linux下完整的用wget命令整站采集网站做镜像 ...
- Mac上利用Aria2加速百度网盘下载
百度网盘下载东西的速度那叫一个慢,特别是大文件,看着所需时间几个小时以上,让人很不舒服,本文记录自己在mac上利用工具Aria2加速的教程,windows下思路也是一样! 科普(可以不看) 这里顺带科 ...
- Use JavaScript to Export Your Data as CSV
原文: http://halistechnology.com/2015/05/28/use-javascript-to-export-your-data-as-csv/ --------------- ...
- [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 ...
- js实现使用文件流下载csv文件
1. 理解Blob对象 在Blob对象出现之前,在javascript中一直没有比较好的方式处理二进制文件,自从有了Blob了,我们就可以使用它操作二进制数据了.现在我们开始来理解下Bolb对象及它的 ...
随机推荐
- 2016 Al-Baath University Training Camp Contest-1 C
Description Rami went back from school and he had an easy homework about bitwise operations (and,or, ...
- datagrid 重写属性
$.fn.panel.defaults.loadMsg = '数据加载中请稍后--'; //设置默认的分页参数 if ($.fn.datagrid) { $.fn.datagrid.defaults. ...
- VS2015使用技巧 为什么我们可以输入cw后按两下tab键出现console.writeline
镇场诗: 大梦谁觉,水月中建博客.百千磨难,才知世事无常. 今持佛语,技术无量愿学.愿尽所学,铸一良心博客.------------------------------------------ 为什么 ...
- WinCE开机Logo的实现(USB下载图片到nandflash)
WinCE开机启动Logo使用Eboot读取NandFlash中的图片数据,然后显示的方式.对于开机logo的方式网友http://jazka.blog.51cto.com/809003/664131 ...
- BZOJ 1835 基站选址(线段树优化DP)
题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1835 题意:有N个村庄坐落在一条直线上,第 i(i>1)个村庄距离第1个村庄的距离 ...
- 【转载】Ogre的内存分配策略
原文:Ogre的内存分配策略 读这个之前,强烈建议看一下Alexandrescu的modern c++的第一章关于policy技术的解释.应该是这哥们发明的,这里只是使用. 首先列出涉及到的头文件:( ...
- 终止jQuery的$.ajax方法abort
最近遇到,如果用户频繁点击ajax请求,有两个问题: 1,如果连续点击了5个ajax请求,前4个其实是无效的,趁早结束节省资源. 2,更严重的问题是:最后一个发送的请求,响应未必是最后一个,有可能造成 ...
- Codeforces Round #243 (Div. 2) B(思维模拟题)
http://codeforces.com/contest/426/problem/B B. Sereja and Mirroring time limit per test 1 second mem ...
- 3.mybatis注解
在上篇2.mybatis入门实例(一) 连接数据库进行查询的基础上 1.添加Mapper接口:UserMapper接口,并使用mybatis的注解 import java.util.List; imp ...
- Java I/O NIO学习
给出一个学习的链接讲的很全.. http://ifeve.com/java-nio-all/ 上边的是中文翻译的这里是原地址:http://tutorials.jenkov.com/java-nio/ ...