Last active
October 3, 2019 08:12
-
-
Save alexmnv/43813725699be96ab50b to your computer and use it in GitHub Desktop.
Silex/Symfony - output CSV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | |
)); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome stuff! Thanks for this.