从Codeigniter导出CSV文件

时间:2014-12-05 18:43:15

标签: php mysql codeigniter csv

我在帮助程序中使用csv_helper.php文件进行导出。它正在从mysql中获取结果但只显示结果而不是下载! 这是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 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,1 54,aws,abc @ yahoo .com,12,12,阿富汗,Kapisa ,,,“资源人”,0 55,onti,ontika @ ya.com,12,12 ,, 0 ,,,“注册用户”,1 56,edf,df @ abc.com,12,12,阿尔巴尼亚,Bulqize ,, dewde,“管理员用户”,1 58,meena,meena @ abc.com ,,,,,,,“注册用户”,0 61,nisat,nisat @ abc.com ,,,,,,,“注册用户”,0

但不下载!试过Chrome和mozilla ......

怎么做???

提前谢谢!

1 个答案:

答案 0 :(得分:1)

尝试修改array_to_csv()函数中的标题:

// 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);
}

谢谢,

安德鲁