write into csv file in php

前端 未结 4 968
再見小時候
再見小時候 2021-01-06 17:43

I have to create and write into a csv file. Now I tried with the following code:

header(\'Content-type: application/octet-stream\');  
header(\'Content-dispo         


        
4条回答
  •  有刺的猬
    2021-01-06 18:25

    header() needs to come after the array and fopen

    try this:

    $data = array (
        'aaa,bbb,ccc,ffffdd',
        '123,456,789',
        '"aaa","bbb"');
    $fp = fopen('php://output', 'w+'); 
    header('Content-type: application/octet-stream');  
    header('Content-disposition: attachment; filename="data.csv"'); 
    foreach($data as $line){
        $val = explode(",",$line);
        fputcsv($fp, $val);
    }
    fclose($fp);
    

提交回复
热议问题