I am trying to create and download a csv file from php, and it is not working for me. I did what I always do in these cases, I went to the basics, and I eliminated the query in the database and others, to go step by step and discover the problem. So the first test was just to create the csv in a fixed path, which did work for me. When I make the changes to download it instead of using fixed path, it just doesn't work and I have no idea what the problem is.
The code that doesn't work is:
<?php
$arreglo[0] = array("Nombre","Apellido","Animal","Fruto");
$arreglo[1] = array("Juan","Juarez","Jirafa","Jicama");
$arreglo[2] = array("Maria","Martinez","Mono","Mandarina");
$arreglo[3] = array("Esperanza","Escobedo","Elefante","Elote");
$filename ="prueba.csv";
$delimitador = ",";
$encapsulador = '"';
//asigno el header para descargar el archivo en lugar de escribirlo
header('Content-Encoding: UTF-8');
header('Content-Type: text/csv; charset:UTF-8');
header('Content-Disposition: attachment; filename="' . $filename . '";');
error_log("se supone que estoy en eso");
$f = fopen('php://memory', 'w');
foreach ($arreglo as $linea) {
fputcsv($f, $linea, $delimitador, $encapsulador);
}
rewind($f);
fclose($f);
?>
The code that works fine is:
<?php
$arreglo[0] = array("Nombre","Apellido","Animal","Fruto");
$arreglo[1] = array("Juan","Juarez","Jirafa","Jicama");
$arreglo[2] = array("Maria","Martinez","Mono","Mandarina");
$arreglo[3] = array("Esperanza","Escobedo","Elefante","Elote");
$ruta ="prueba.csv";
$delimitador = ",";
$encapsulador = '"';
error_log("se supone que estoy en eso");
$f = fopen($ruta, 'w');
foreach ($arreglo as $linea) {
fputcsv($f, $linea, $delimitador, $encapsulador);
}
rewind($f);
fclose($f);
?>