Skip to content

Instantly share code, notes, and snippets.

@rettal
Created October 13, 2015 09:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rettal/f3d0646a7a4ca5458c28 to your computer and use it in GitHub Desktop.
Save rettal/f3d0646a7a4ca5458c28 to your computer and use it in GitHub Desktop.
function convert_to_csv($input_array, $output_file_name, $delimiter)
{
/** open raw memory as file, no need for temp files, be careful not to run out of memory thought */
$f = fopen('php://memory', 'w');
/** loop through array */
foreach ($input_array as $line) {
/** default php csv handler **/
fputcsv($f, $line, $delimiter);
}
/** rewrind the "file" with the csv lines **/
fseek($f, 0);
/** modify header to be downloadable csv file **/
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
/** Send file to browser for download */
fpassthru($f);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment