0

So i have two files one reads an exsisting CSV edits and changes some values and then writes a new file. My goal is it that the new csv is then directly downloadable. It works in two seperate files but i have to combine them... it does not work.

´

If i have the two seperate it does work but when i combined them like this it did not work:

$file_name = 'sample.csv';
$fp = fopen($file_name, 'w');
foreach ($infos as $info) {
    fputcsv($fp,explode(';',$info));
}
fclose($fp);
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=sample.csv');
readfile('sample.csv');


//FIRST FILE (JUST THE LAST BIT WHERE THE FILE IS CREATED)

    $file_name = 'sample.csv';
$fp = fopen($file_name, 'w');
foreach ($infos as $info) {
    fputcsv($fp,explode(';',$info));
}
fclose($fp);
echo "<script>window.open('.../Leon/downloadFile.php')</script>";
?>

    //SECOND FILE AT (.../Leon/downloadFile.php)
<?php
header('Content-type: application/csv');
header('Content-Disposition: attachment; filename=sample.csv');
readfile('sample.csv');
?>
  • What about it does “not work”? – 04FS May 23 '19 at 08:16
  • Instead of writing to an actual physical file and then trying to read from that again, you can also write to PHP’s output stream directly, https://stackoverflow.com/a/40562178/10955263 – 04FS May 23 '19 at 08:16
  • At first sight, when you combine those two in the same file, you already echo a result at `echo "";` and after that you are trying to set some header information. The headers can't be changed after an output has been already set. Besides that, I can see that you are not echoing your `window.open` function at first part. Consider doing it and come again with a result. – Alcaeus D May 23 '19 at 09:48

0 Answers0