Note that there are some explanatory texts on larger screens.

plurals
  1. POCodeigniter force download CSV file bug
    text
    copied!<p>Here's the problem I wanted to convert my data into csv format and download it. everything is fine, until the csv file that i had downloaded and there's a little bug on the file which there will be a space at the first line of the file. </p> <p>for example Before force download<br/></p> <pre> "name","age",<br/> "brad pit","40",` </pre> <p>After force download</p> <pre> <br/> "name","age",<br/> "brad pit","40", </pre> <p>The csv file that i had downloaded and I try to open wit my excel will appear like this<br/> "name" |age<br/> brad pit|40</p> <p>I believe that is because of the csv file that i had downloaded appeared an external space line in the first line of the data. </p> <p>Here's the code</p> <pre> //write csv data $data = $this->dbutil->csv_from_result($query, $delimiter); //create random file name $name = rand().'_salesperson_data_'.date('d-m-y').'.csv'; if ( ! write_file('./csv/'.$name, $data)) { echo 'Unable to write the CSV file'; } else { //perform download $file = file_get_contents("./csv/".$name); // Read the file's contents $filename = 'salesperson_data_'.date('d-m-y').'.csv'; force_download($filename, $file); } </pre> <p>source of force_download()</p> <pre> if ( ! function_exists('force_download')) { function force_download($filename = '', $data = '') { if ($filename == '' OR $data == '') { return FALSE; } // 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)); } 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); } } </pre> <p>i thought TRIM will be last solution for me and I try to put any where possible but is stil the same. I couldn't found any solution for this problem. Please help. this stuck me for 2days already. </p> <p>Thanks in advanced. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload