write into csv file in php

前端 未结 4 1017
再見小時候
再見小時候 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:11

    Try this:

    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=data.csv');
    
    $data = array (
        'aaa,bbb,ccc,ffffdd',
        '123,456,789',
        '"aaa","bbb"');
    
    
    $fp = fopen('php://output','w');
    foreach ($data as $line) {
        fputcsv($fp, array_map('utf8_decode',array_values($line)), ',', '"');
    }
    fclose($fp);
    

提交回复
热议问题