2014-12-05 196 views
1

我在助手中使用csv_helper.php文件進行導出。它正在抓取mysql的結果,但只顯示結果而不是下載! 這裏的csv_helper從Codeigniter導出CSV文件

<?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 as $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() as $name) 
      { 
       $line[] = $name; 
      } 
      $array[] = $line; 
     } 

     foreach ($query->result_array() as $row) 
     { 
      $line = array(); 
      foreach ($row as $item) 
      { 
       $line[] = $item; 
      } 
      $array[] = $line; 
     } 

     echo array_to_csv($array, $download); 
    } 
} 

而這裏的控制器功能:

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

並在視圖頁面則顯示結果: 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,154,aws,abc @ yahoo.com,12,12,阿富汗,Kapisa ,,,「資源人「,0 55,onti,ontika @ ya.com,12,12,,0 ,,,」註冊用戶「,156,edf,df @ abc.com,12,12,阿爾巴尼亞,Bulqize ,,露,「Admin User」,158,meena,meena @ abc.com ,,,,,,,「註冊用戶」, 61,nisat @ abc.com ,,,,,,,「註冊用戶」,0

但沒有下載!試過Chrome和mozilla兩者....

怎麼辦?

預先感謝您!

+1

我認爲它應該是'Content-Disposition:attachment',而不是'attachchement'。 – izstas 2014-12-05 18:45:36

+0

已編輯...但仍然是相同的情況:( – 2014-12-05 18:47:47

+0

您可以驗證標題設置是否正確?在大多數瀏覽器中的「網絡」檢查器顯示請求和響應標頭 – tadman 2014-12-05 18:51:08

回答

1

嘗試修改標題中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'); 

然後輸出部分後,添加退出:

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

,或者嘗試使用CodeIgniter的內置功能:

public function exportUser() { 
    // Load database and 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); 
} 

謝謝,

Andrew

+0

只顯示結果....沒有下載:( – 2014-12-05 19:48:19

+0

我修改了我的答案,包括修改你的csv_helper文件的選項 – versalle88 2014-12-05 19:52:06

+0

我改變了csv_helper。 php 同樣的情況!! – 2014-12-05 19:53:58