php - Export CSV file from Codeigniter -


i using csv_helper.php file in helpers exporting. grabing results mysql showing results instead of downloading ! here's csv_helper

<?php  if ( ! defined('basepath')) exit('no direct script access allowed');  if ( ! function_exists('array_to_csv')) {     function array_to_csv($array, $download = "")     {         if ($download != "")         {                header('content-type: application/csv');             header('content-disposition: attachment; filename="' . $download . '"');         }                ob_start();         $f = fopen('php://output', 'w') or show_error("can't open php://output");         $n = 0;              foreach ($array $line)         {             $n++;             if ( ! fputcsv($f, $line))             {                 show_error("can't write line $n: $line");             }         }         fclose($f) or show_error("can't close php://output");         $str = ob_get_contents();         ob_end_clean();          if ($download == "")         {             return $str;             }         else         {                echo $str;         }            } }  if ( ! function_exists('query_to_csv')) {     function query_to_csv($query, $headers = true, $download = "")     {         if ( ! is_object($query) or ! method_exists($query, 'list_fields'))         {             show_error('invalid query');         }          $array = array();          if ($headers)         {             $line = array();             foreach ($query->list_fields() $name)             {                 $line[] = $name;             }             $array[] = $line;         }          foreach ($query->result_array() $row)         {             $line = array();             foreach ($row $item)             {                 $line[] = $item;             }             $array[] = $line;         }          echo array_to_csv($array, $download);     } } 

and here's controller function:

public function exportuser() {     $this->load->database();     $query = $this->db->get('user');     $this->load->helper('csv');     query_to_csv($query, true, 'toto.csv'); } 

and in view page showing results: user_id,user_name,user_email,user_pass,user_phone,user_country,user_city,user_zip,user_address,user_type,user_status 53,abcdef,abcd@yahoo.com,12,1,,0,,,student,1 54,aws,abc@yahoo.com,12,12,afghanistan,kapisa,,,"resource person",0 55,onti,ontika@ya.com,12,12,,0,,,"registered user",1 56,edf,df@abc.com,12,12,albania,bulqize,,dewde,"admin user",1 58,meena,meena@abc.com,,,,,,,"registered user",0 61,nisat,nisat@abc.com,,,,,,,"registered user",0

but not downloading ! tried chrome , mozilla both....

what do???

thank in advance !

try modifying headers in array_to_csv() funtion:

// disable caching $time = gmdate('d, d m y h:i:s'); header('expires: tue, 03 jul 2001 06:00:00 gmt'); header('cache-control: max-age=0, no-cache, must-revalidate, proxy-revalidate'); header('last-modified: ' . $time . ' gmt');  // force download header('content-type: application/force-download'); header('content-type: application/octet-stream'); header('content-type: application/download');  // set encoding header('content-disposition: attachment;filename=' . $download); header('content-transfer-encoding: binary'); 

then after output section, add exit:

if ($download == "") {     return $str;     } else {        echo $str; } exit; 

or try using codeigniter's built-in functions:

public function exportuser() {     // load database , query     $this->load->database();     $query = $this->db->get('user');      // load database utility class     $this->load->dbutil();     // create csv output     $data = $this->dbutil->csv_from_result($query);      // load download helper     $this->load->helper('download');     // stream download     force_download('toto.csv', $data); } 

thanks,

andrew


Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -