Create a CSV File for a user in PHP

前端 未结 19 2112
故里飘歌
故里飘歌 2020-11-22 11:52

I have data in a MySQL database. I am sending the user a URL to get their data out as a CSV file.

I have the e-mailing of the link, MySQL query, etc. covered.

<
19条回答
  •  无人共我
    2020-11-22 12:14

    Simple method -

    $data = array (
        'aaa,bbb,ccc,ffffdd',
        '123,456,789',
        '"aaa","bbb"');
    
    $fp = fopen('data.csv', 'wb');
    foreach($data as $line){
        $val = explode(",",$line);
        fputcsv($fp, $val);
    }
    fclose($fp);
    

    So each line of the $data array will go to a new line of your newly created CSV file. It only works only for PHP 5 and later.

提交回复
热议问题