Forked from vrushank-snippets/PHP : Array To CSV - Download CSV File
Created
August 30, 2018 10:03
-
-
Save Ashley-Upson/3dd59888f0637025cc42cf0e5dc0e755 to your computer and use it in GitHub Desktop.
PHP : Array To CSV - Download CSV File
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
$fileName = 'Billing-Summary.csv'; | |
header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); | |
header('Content-Description: File Transfer'); | |
header("Content-type: text/csv"); | |
header("Content-Disposition: attachment; filename={$fileName}"); | |
header("Expires: 0"); | |
header("Pragma: public"); | |
$fh = @fopen( 'php://output', 'w' ); | |
$headerDisplayed = false; | |
foreach ( $results as $data ) { | |
// Add a header row if it hasn't been added yet | |
if ( !$headerDisplayed ) { | |
// Use the keys from $data as the titles | |
fputcsv($fh, array_keys($data)); | |
$headerDisplayed = true; | |
} | |
// Put the data into the stream | |
fputcsv($fh, $data); | |
} | |
// Close the file | |
fclose($fh); | |
// Make sure nothing else is sent, our file is done | |
exit; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment