Skip to content

Instantly share code, notes, and snippets.

@delor34n delor34n/arrayToCsv.php
Last active Jul 10, 2017

Embed
What would you like to do?
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
You can’t perform that action at this time.