Create a CSV File for a user in PHP

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

    header("Content-Type: text/csv");
    header("Content-Disposition: attachment; filename=file.csv");
    
    function outputCSV($data) {
      $output = fopen("php://output", "wb");
      foreach ($data as $row)
        fputcsv($output, $row); // here you can change delimiter/enclosure
      fclose($output);
    }
    
    outputCSV(array(
      array("name 1", "age 1", "city 1"),
      array("name 2", "age 2", "city 2"),
      array("name 3", "age 3", "city 3")
    ));
    

    php://output
    fputcsv

提交回复
热议问题