利用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); } }