Create a CSV File for a user in PHP

前端 未结 19 2036
故里飘歌
故里飘歌 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:16

    You can simply write your data into CSV using fputcsv function. let us have a look at the example below. Write the list array to CSV file

    $list[] = array("Cars", "Planes", "Ships");
    $list[] = array("Car's2", "Planes2", "Ships2");
    //define headers for CSV 
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=file_name.csv');
    //write data into CSV
    $fp = fopen('php://output', 'wb');
    //convert data to UTF-8 
    fprintf($fp, chr(0xEF).chr(0xBB).chr(0xBF));
    foreach ($list as $line) {
        fputcsv($fp, $line);
    }
    fclose($fp);
    

提交回复
热议问题