Skip to content

Instantly share code, notes, and snippets.

@vijinho
Last active February 22, 2016 15:12
Show Gist options
  • Save vijinho/38d24d04c39d28a40b18 to your computer and use it in GitHub Desktop.
Save vijinho/38d24d04c39d28a40b18 to your computer and use it in GitHub Desktop.
Convert an array to a csv file
<?php
$filename = 'report_period_' . date('Y-m-d', $time) . '.csv';
$file = sys_get_temp_dir() . '/' . $filename;
$fp = fopen($file, 'w');
foreach ($csvData as $r) {
fputcsv($fp, $r);
}
fclose($fp);
$size_in_bytes = filesize($file);
$ContentType = 'Content-type: application/vnd.ms-excel';
$ContentLength = "Content-Length: $size_in_bytes";
$ContentDisposition = "Content-Disposition: attachment; filename=\"$filename\"";
header($ContentType);
header($ContentLength);
header($ContentDisposition);
readfile($file);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment