0

I am trying to output an array to CSV with PHP using the below code. It is, however outputting the entire HTML page to the CSV. How can I write this so it only outputs the array to the CSV and not the code of the page?

I believe the problem is to do with:

$f = fopen('php://output', 'w');
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    // open raw memory as file so no temp files needed, you might run out of memory though
    $f = fopen('php://output', 'w');
    // loop over the input array
    foreach ($array as $line) { 
        // generate csv lines from the inner arrays
        fputcsv($f, $line, $delimiter); 
    }
    // reset the file pointer to the start of the file
    fseek($f, 0);
    // tell the browser it's going to be a csv file
    header('Content-Type: application/csv');
    // tell the browser we want to save it instead of displaying it
    header('Content-Disposition: attachment; filename="'.$filename.'";');
    // make php send the generated csv lines to the browser
    fpassthru($f);
}

array_to_csv_download($data_array);
6
  • @mcook16 Your code is working fine on my machine. Which Web server are you using? Maybe your Web server is missing a MIME type configuration for CSV files. Commented Aug 14, 2020 at 22:43
  • Is there some other code in the same script, apart from what you've shown? Any other PHP, any other HTML? Commented Aug 14, 2020 at 22:49
  • Content-Type does not had to be text/csv?? Commented Aug 14, 2020 at 23:11
  • @ADyson, yes, there is. There is a database query, and some array manipulation. Commented Aug 14, 2020 at 23:34
  • 1
    It sounds like there must be some HTML output in there as well as what you've mentioned. If your page contains other content then you need to arrange (e.g. with some if statements) it so that can never be output at the same time as the CSV data. Either that or move this functionality into a separate script which is purely dedicated to generating the CSV Commented Aug 15, 2020 at 8:11

1 Answer 1

1

You are sending output before your headers.

You can simplify to:

<?php
function array_to_csv_download($array, $filename = "export.csv", $delimiter=";") {
    header('Content-Type: application/csv');
    header('Content-Disposition: attachment; filename="'.$filename.'";');
    $f = fopen('php://output', 'w');
    foreach ($array as $line)
        fputcsv($f, $line, $delimiter);
}

Make sure you do not output anything else prior to those header calls.

Sign up to request clarification or add additional context in comments.

2 Comments

This fixes the HTML prior to the function, the problem I have now is I have a HTML form below this, and now that HTML is being added to the CSV. Any ideas how to fix that, or will i need to stick to a dedicated page for the CSV that only runs this script?
You could simply exit before the close of the function.

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.