您好,欢迎访问一九零五行业门户网

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

利用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); }}
其它类似信息

推荐信息