简体   繁体   中英

PHP download CSV

Stuck on what is likely a silly problem and only posting after reading several related threads.

Have a page with a lot going on, one of the form options I'm trying to add is so the user can select to download array results in CSV. Problem is HTML header info is coming through in addition to the CSV data I want.

Code is:

function Array2Csv($result, $filename){
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=' .$filename);
    $output = fopen('php://output', 'w');
    while($row = mysql_fetch_assoc($result)) {
            fputcsv($output, $row,'|','"');
    }
}

Problem is the result file includes BOTH undesired markup (headers and scripting references) in addition to the CSV itself. Desired output should only include the CSV data.

You have send the the header before any output was send. Disable view and layout.

See also http://php.net/manual/en/function.header.php

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