Use PHP CI force_download ($filename, $data) to download. csv file to resolve file name garbled, file content garbled

Source: Internet
Author: User
Tags money back
Use PHP CI force_download ($filename, $data) to download. csv file to resolve file name garbled, file content garbled. Did a long time finally know a good solution.

1. Loading auxiliary functions

$this->load->helper (' Download ');  Download auxiliary function $this->load->helper (' string ');    Character Encoding conversion auxiliary turn book

2.force_download ($filename, $data) through its code can know that $data must be a string, if not a string error. is an array, must be converted to a string, with the function implode can, the function usage See official website, more my project code is as follows.

Public function Download () {//Output Excel file Header//resolve ie,firework such as browser file name garbled problem $filename = ' split data. csv '; $newarray =array (); The result is a two-dimensional array if ($this->input->get ()!== false) {$conn = $this->getformdata ();}  Output Excel column name information $head = array (' ID ', ' return money ', ' Money Back Amount (min) ', ' Adjust channel cost ', ' return time ', ' Import so time ', ' Channel name ', ' contract ID '); foreach ($head as $i = $v {//CSV Excel supports GBK encoding, be sure to convert otherwise garbled $head [$i] = UTF2GBK ($v);} $newarray []=implode (', ', $head); $sql = "SELECT * from Sinapay_boss_income where {$conn} ORDER by income_status ASC, boss_in come_id desc "; $query = $this->db->query ($sql);//Counter $cnt = 0;//every $limit line, refresh the output buffer, not too big or too small $limit = 8000;fo Reach ($query->result_array () as $row) {$cnt ++;if ($limit = = $cnt) {//Refresh output buffer to prevent problems caused by excessive data ob_flush (); flush (); $cnt = 0;} Read table Data $content = Array (), $content [] = $row [' boss_income_id ']; $content [] = $row [' Income_num ']; $content [] = $row [' Inco Me_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. After the test I found that the Internet Explorer file name is garbled, Firefox browser filename is garbled. So the following changes to the auxiliary function, the following add the background color of the code is I added.

if (! function_exists (' force_download ')) {function force_download ($filename = ', $data = ') {if ($filename = = "OR $data = = ") {return FALSE;}               IE browser address file name is garbled $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 typeif (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 itif (! isset ($mimes [$extension])) {$mime = ' application/octet-stream ';} else{$mime = (Is_array ($mimes [$extension])? $mimes [$extension][0]: $mimes [$extension];} Generate the server Headersif (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") {//fix Firefox filename garbled 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);}}
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.