简体   繁体   中英

Download a csv using php

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);

?>

It doesn't seem like this method will work. From the PHP manual: "php://memory and php://temp are not reusable, ie after the streams have been closed there is no way to refer to them again." After fclose the memory-file is gone. Also, the memory is never attached to the $filename so, for two reasons, you are passing a filename to a file that doesn't exist.

You could change to:

<?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 = '"';

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://output", 'w');
foreach ($arreglo as $linea) {
fputcsv($f, $linea, $delimitador, $encapsulador);
}

or if you want to save the file:

$f = fopen( $filename, 'w');
foreach ($arreglo as $linea) {
fputcsv($f, $linea, $delimitador, $encapsulador);
}
fclose($f);

//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 . '";');
readfile( $filename); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM