Skip to content

Instantly share code, notes, and snippets.

@delor34n
Last active July 10, 2017 20:09
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 delor34n/530cae99a87b40a5435d0d88de4ff77e to your computer and use it in GitHub Desktop.
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
<?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