-1

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

?>

1 Answers1

0

It doesn't seem like this method will work. From the PHP manual: "php://memory and php://temp are not reusable, i.e. 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); 
tangledtrees
  • 121
  • 5
  • Ok, I change memory by outpu, and comment rewind and fclose, but it didn't work. So I try the other option, but it didn't work either – María Del Pilar Fernández Jun 09 '22 at 20:57
  • I edited the answer to put in the whole solution php. I just tried it exactly as written and it worked on my server. Did you remove the '?> ' at the end of your file? – tangledtrees Jun 09 '22 at 21:19
  • I copy the same code you put, it didn't work. I think there will be something wrong in the header definition? – María Del Pilar Fernández Jun 09 '22 at 21:28
  • No, the header has to be ok, since I'm looking at the downloaded file right now. Are you getting an empty file or is it not prompting you to save it at all? – tangledtrees Jun 09 '22 at 21:51
  • it is not prompting me to save, it just don't do anything – María Del Pilar Fernández Jun 09 '22 at 23:45
  • If you put: printf( "Downloading...
    ") ; as the first line, this should cause the csv to print to the screen. If that doesn't work something else is the issue. I'm having trouble debugging it because it works as is here.
    – tangledtrees Jun 10 '22 at 00:05
  • perhaps your old version is being cached. – tangledtrees Jun 10 '22 at 00:12
  • I really apreciate your help, but I didn't understand where I have to put the printf, I put it before fopen and nothing happen – María Del Pilar Fernández Jun 10 '22 at 00:24
  • I would put it as the first line for debugging, but that's ok, no output means either the new php isn't being executed or there is a bug which the server is set to hide. Try making the file just one line: printf( "anything
    ") ; if that doesn't display, I would guess the old file is being cached by your server.
    – tangledtrees Jun 10 '22 at 00:35
  • ok, I get what you are trying to do, but this is the problem, I keep only 2 lines : printf( "Downloading...
    ") ; error_log("solo el printf"); I change the error_log message sometimes so I can be sure the new version is running, by look this message in php error log, and it is loading this new file, but printf don't print anything
    – María Del Pilar Fernández Jun 10 '22 at 01:14
  • Ok, if a simple printf or echo doesn't display anything, then I think it has to be a php configuration error. You can try here: https://stackoverflow.com/questions/7639271/simple-php-echo-code-not-working ... once you can output to the browser, the code above should work. – tangledtrees Jun 10 '22 at 01:24
  • thankas a lot, I will try with that – María Del Pilar Fernández Jun 10 '22 at 14:59