0

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

?>

2 Answers 2

1

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', ',');
Sign up to request clarification or add additional context in comments.

3 Comments

thanks a lot. 1- I want the values appear on the screen, so I kind of need the echoes, but putting it, stops the download of the file. + the values are not seperated by commas in the generated file.
You must understand that there is only one option - either download file or view webpage. There's NO way to do both in php.
If you need values separated by commas, then as @gmc said - pass $input_array to fputcsv.
0

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.

1 Comment

Seriously - you put all values from one array $input_array to another array $fields? Why?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.