简体   繁体   中英

PHP code to download CSV file not working

I have the below code, where I generate random numbers and try to download them as a CSV file.

If I try the CSV code snippet alone on a different file it works, but in this code, it just does not work. I do see the random numbers echoed, but it does not download the CSV file.

<?php

$dataarray=array();

function convert_to_csv($input_array, $output_file_name, $delimiter)
{
$temp_memory = fopen('php://memory', 'w');
foreach ($input_array as $line) 
{
fputcsv($temp_memory, $line, $delimiter);
}

fseek($temp_memory, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
fpassthru($temp_memory);
}

for ($i = 0; $i <= 25000; $i++) 
{ 
  $num = (rand(50,110));

  array_push($dataarray,"$num");

  echo "Hearbeat #" .$i . "\t\t ------   " . $num;
  echo "<br>";
}

convert_to_csv($dataarray, 'data_as_csv.csv', ',');

?>

Outputting data before sending headers with header() means that header() calls will have no effect. So it is reasonable not to send anything before headers.

As another answer mentioned - second argument to fputcsv must be array, so I changed the call to fputcsv too.

If you want all values to written to csv file with comma as separator - pass $input_array directly to fputcsv .

So, your code can look like:

<?php
$dataarray=array();

function convert_to_csv($input_array, $output_file_name, $delimiter)
{
$temp_memory = fopen('php://memory', 'w');

// Option 1 - every number will be on a new line
foreach ($input_array as $line) 
{
// add `array($line)` not `$line`
fputcsv($temp_memory, array($line), $delimiter);
}

// Option 2 - all numbers will be in 
// one line with `$delimiter` as separator
fputcsv($temp_memory, $input_array, $delimiter);

fseek($temp_memory, 0);
header('Content-Type: application/csv');
header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
fpassthru($temp_memory);
}

for ($i = 0; $i <= 25000; $i++) 
{ 
  $num = (rand(50,110));
  array_push($dataarray,"$num");

  // this two lines are nor needed
  //echo "Hearbeat #" .$i . "\t\t ------   " . $num;
  //echo "<br>";
}

convert_to_csv($dataarray, 'data_as_csv.csv', ',');

Actually, the problem is in your convert_to_csv function. fputcsv expects the second parameter to be an array, and in you case is a string. You have 2 options depending on what you want:

1) Each number in a separate line: just change the fputcsv function call to: fputcsv($temp_memory, [$line], $delimiter);

2) All numbers in the same line separated by $delimiter :

function convert_to_csv($input_array, $output_file_name, $delimiter)
        {
            $temp_memory = fopen('php://memory', 'w');

            fputcsv($temp_memory, $fields, $delimiter);
            fseek($temp_memory, 0);
            header('Content-Type: application/csv');
            header('Content-Disposition: attachement; filename="' . $output_file_name . '";');
            fpassthru($temp_memory);
        }

And (I'm sure you already know), for the file to be downloaded automatically you must not echo anything.

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