Use phpciforce_download($filename, unzip data‑download .csv file to solve file name garbled and file content garbled

Source: Internet
Author: User
Use phpciforce_download($filename, $data‑download .csv file to solve file name garbled. use php CI force_download ($ filename, $ data) to download .csv file to solve file name garbled and file content garbled. After a long time, I finally learned a good solution.

1. attach auxiliary functions

$ This-> load-> helper ('download'); // download helper functions $ this-> load-> helper ('string'); // character encoding conversion helper book Turning

2. force_download ($ filename, $ data) uses its code to know that $ data must be a string. if it is not a string, an error occurs. Is an array and must be converted to a string. use the implode function. for the usage of this function, see the official website. my project code is as follows.

Public function download () {// output the Excel file header // solves the problem of garbled characters in IE, firework, and other browser file names $ filename = 'Retrieved data .csv '; $ newarray = array (); the result is a two-dimensional array if ($ this-> input-> get ()! = False) {$ conn = $ this-> getformdata ();} // output the name of the Excel column $ head = array ('id', 'remittance no ', 'remittance amount (points) ', 'adjustment channel cost', 'remittance time', 'import SO time', 'Channel name', 'contract ID '); foreach ($ head as $ I =>$ v) {// CSV Excel supports GBK encoding and must be converted, otherwise, garbled code $ 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 ); // counter $ cnt = 0; // refresh the output buffer every $ limit row. do not set it to too large or too small $ limit = 8000; foreach ($ query-> result_array () as $ row) {$ cnt ++; if ($ limit = $ cnt) {// refresh the 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 ['Welcome _ num']; $ content [] = $ row ['Welcome _ amount']; $ content [] = $ row ['modified _ cost']; $ content [] = $ row ['Welcome _ date']; $ content [] = $ row ['write _ so_month ']; $ content [] = utf2gbk ($ row ['Channel _ name']); $ content [] = utf2gbk ($ row ['Welcome _ contract_id ']); $ newarray [] = implode (', ', $ content );} $ newarray = implode ("\ n", $ newarray); force_download ($ filename, $ newarray );}}

3. after the test, I found that the IE browser file name is garbled and the firefox browser file name is garbled. So I made the following changes to the helper function, and I added the background color code below.

If (! Function_exists ('force _ download') {function force_download ($ filename = '', $ data = '') {if ($ filename = ''OR $ data ='') {return FALSE;} // the file name in the ie browser is garbled $ filename = urlencode ($ filename ); $ filename = str_replace ("+", "% 20", $ filename); // Try to determine if the filename between Des a file extension. // We need it in order to set the MIME typeif (FALSE === strpos ($ filename ,'. ') {return FALSE;} // Grab t He 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 the 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.