<?php
function export_array_to_csv($data)
{
  $output = fopen("php://output", 'w');
  foreach($data as $value)
    fputcsv($output, $value);
  fclose($output);
}

header('Content-Encoding: UTF-8');
header('Content-type: text/csv; charset=UTF-8');
$date = date('Y-m-d-H:i:s');
header("Content-Disposition: attachment; filename=csv-export-{$date}.csv");
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: no-cache');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Cache-Control: max-age=0');

export_array_to_csv($array);
Published in: PHP
Download

Related snippets