Last active
July 10, 2017 20:09
-
-
Save delor34n/530cae99a87b40a5435d0d88de4ff77e to your computer and use it in GitHub Desktop.
función que permite exportar un array y exportarlo a un archivo CSV
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 | |
/* | |
* @fuente: http://stackoverflow.com/a/16251849 | |
*/ | |
function generateFile($datos, $filename = "export.csv", $delimiter=";") { | |
$f = fopen("/tmp/$filename", 'w'); | |
// loop over the input array | |
foreach ($datos 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