简体   繁体   中英

During CSV downloading - Warning: Cannot modify header information - headers already sent by (output started at

The following small demo code is running successfully

<?php
$list = array
(
"ID,Name,Preview,Payout",
"Peter,Griffin,Oslo,Norway",
"Glenn,Quagmire,Oslo,Norway",
);
$today_doc=date("Y-m-d").".csv";
$file = fopen($today_doc,"w");

foreach ($list as $line)
  {
  fputcsv($file,explode(',',$line));
  }
  fclose($file); 
// We'll be outputting a csv
header('Content-type: text/csv');
// It will be called contacts.csv
header('Content-Disposition: attachment; filename="'.$today_doc.'"');
// The PDF source is in original.csv
readfile($today_doc);?>

I used the above demo code to my script then its not working. Saying-> Warning: Cannot modify header information - headers already sent by (output started at

    $selected_ids=$_POST['selected_id'];
    $xyz="";
    $list = array
    ("ID,Offer Name,Emp,salary");
    $i=0;
    foreach($selected_ids as $id)
        {
            $xyz.=$id.',';
        }
        $xyz=trim($xyz,',');
    $extract=mysqli_query($con,"SELECT * FROM `emp_record` WHERE id in ($xyz)") or die(mysqli_error($con));
    while($row = mysqli_fetch_array($extract))
    {
        $i++;
        $list[$i]=$row["id"].','.$row["name"].','.$row["emp"].','.$row["salary"];
    }
$today_doc=date("Y-m-d").".csv";
$today_doc="contacts.csv";
$file = fopen($today_doc,"w");
foreach ($list as $line)
  {
  fputcsv($file,explode(',',$line));
  }
  fclose($file);
  //  We'll be outputting a csv
 header('Content-type: text/csv');
// It will be called contacts.csv
header('Content-Disposition: attachment; filename=contacts.csv');
//The PDF source is in original.csv
readfile("contacts.csv");
}

Can anyone please help to resolve the issue.

Put header functions before any echo/output:

<?php
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=contacts.csv');

EDITED:

oh, this was not your question, but if you want like this you should echo this headers "header('Content-type: text/csv');header('Content-Disposition: attachment; filename=contacts.csv');" and then load csv from file in some var eg. "$csv", and then Just echo content, something like:

<?php
header('Content-type: text/csv');
header('Content-Disposition: attachment; filename=contacts.csv');
$csv = file_get_contents('YOUR  PATH');
echo $csv;
exit;

You will always get this error "Warning: Cannot modify header information - headers already sent" when

  1. you leave a blank line before

  2. if the call was an ajax call and you echoed some characters before calling the header.

Just mak sure the is no character before

You can add:

ob_start() at start

and

ob_flush() at end

Example:

<?php ob_start(); ?>
<?php
    header('Content-Type: text/csv; charset=utf-8');
    header('Content-Disposition: attachment; filename=export.csv');
     .......
     ob_flush();
?>

Note

If you want, you can use:

ob_start("ob_gzhandler")

ob_gzhandler() is intended to be used as a callback function for ob_start() to help facilitate sending gz-encoded data to web browsers that support compressed web pages.

You see:

PHP net ob_start

and

PHP net ob_gzhandler

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