简体   繁体   中英

Download CSV in codeigniter

    $ci =& get_instance();
    $ci->load->dbutil();
    $result =  $ci->dbutil->csv_from_result($data);
    $filename = "sample.csv";
    $fp = fopen('php://output', 'w');
    header('Content-type: application/csv');
    header('Content-Disposition: attachment; filename='.$filename.'";'); 
    header('Cache-Control: must-revalidate,post-check=0, pre-check=0');
    header("Pragma: no-cache");
    header("Expires: 0");
    foreach ($final_res as $line){ 
        fputcsv($fp,explode(',',$line)); 
    }         
    fclose($fp);          
    exit;

I am not able to download the CSV file. I couldn't find any errors. please help

Try changing header in your code.

header('Content-Description: File Transfer');
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename='.$filename);
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($filename));

Why not just use force_download() helper function of CI. LINK

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM