Skip to content

Instantly share code, notes, and snippets.

@alexmnv
Last active October 3, 2019 08:12
Show Gist options
  • Save alexmnv/43813725699be96ab50b to your computer and use it in GitHub Desktop.
Save alexmnv/43813725699be96ab50b to your computer and use it in GitHub Desktop.
Silex/Symfony - output CSV
function outputCSV(Application $app, array $recs, $filename)
{
$stream = function() use ($recs) {
$output = fopen('php://output', 'w');
foreach ($recs as $rec)
{
fputcsv($output, $rec);
}
fclose($output);
};
return $app->stream($stream, 200, array(
'Content-Type' => 'text/csv',
'Content-Description' => 'File Transfer',
'Content-Disposition' => 'attachment; filename="'.urlencode($filename).'',
'Expires' => '0',
'Cache-Control' => 'must-revalidate',
'Pragma' => 'public',
));
}
@DazDotOne
Copy link

Awesome stuff! Thanks for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment