简体   繁体   English

PHP代码下载CSV文件不起作用

[英]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. 我有以下代码,在其中生成随机数,然后尝试将其下载为CSV文件。

If I try the CSV code snippet alone on a different file it works, but in this code, it just does not work. 如果我在其他文件上尝试单独使用CSV代码段,则可以使用,但是在此代码中,它无效。 I do see the random numbers echoed, but it does not download the CSV file. 我确实看到了随机数,但没有下载CSV文件。

<?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. 与发送报头之前输出数据header()是指header()调用将没有任何效果。 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. 提到的另一个答案fputcsv第二个参数必须是数组,因此我也将调用更改为fputcsv

If you want all values to written to csv file with comma as separator - pass $input_array directly to fputcsv . 如果您希望所有值都以逗号作为分隔符写入csv文件, $input_array直接传递给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. 实际上,问题出在您的convert_to_csv函数中。 fputcsv expects the second parameter to be an array, and in you case is a string. fputcsv期望第二个参数为数组,在您的情况下为字符串。 You have 2 options depending on what you want: 您有2种选择,具体取决于您想要的:

1) Each number in a separate line: just change the fputcsv function call to: fputcsv($temp_memory, [$line], $delimiter); 1)每个数字放在单独的行中:只需将fputcsv函数调用更改为: fputcsv($temp_memory, [$line], $delimiter);

2) All numbers in the same line separated by $delimiter : 2)同一行中的所有数字都用$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. 并且(我确定您已经知道),对于要自动下载的文件,您不得echo任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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