-
-
Save jimboobrien/ae99c9e9156223d3a406538e7f53fb34 to your computer and use it in GitHub Desktop.
Takes an array of data and create a csv file that will automatically download
This file contains hidden or 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
<?php | |
/** | |
* Takes an array of data and creates a csv file that will automatically download | |
*/ | |
function downloadable_csv( $data, $filename = 'data_csv' ){ | |
/* | |
Sample Data Format: | |
array( | |
'headings' => array(), | |
'data' => array( | |
array() | |
) | |
); | |
*/ | |
header('Content-Type: text/csv; charset=utf-8'); | |
header('Content-Disposition: attachment; filename='.$filename.'.csv'); | |
// Create a file pointer connected to the output stream | |
$output = fopen('php://output', 'w'); | |
// Output the column headings | |
fputcsv($output, $data['headings']); | |
// Loop over the rows, outputting them | |
foreach( $data['data'] as $row ){ | |
fputcsv($output, $row, ',', '"'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment