Last active
April 2, 2016 15:06
-
-
Save alexjeen/3ccdc08a1243defa7929 to your computer and use it in GitHub Desktop.
Array to CSV PHP
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 _array_to_csv_download($array, $filename = "export.csv", $delimiter=",") { | |
// open raw memory as file so no temp files needed, you might run out of memory though | |
$f = fopen('php://memory', 'w'); | |
// loop over the input array | |
foreach ($array as $line) { | |
// generate csv lines from the inner arrays | |
fputcsv($f, $line, $delimiter); | |
} | |
// reset the file pointer to the start of the file | |
fseek($f, 0); | |
// tell the browser it's going to be a csv file | |
header('Content-Type: application/csv'); | |
// tell the browser we want to save it instead of displaying it | |
header('Content-Disposition: attachment; filename="'.$filename.'";'); | |
// make php send the generated csv lines to the browser | |
fpassthru($f); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment