简体   繁体   English

在CSV下载期间-警告:无法修改标头信息-已发送的标头(输出始于

[英]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');" 哦,这不是您的问题,但是,如果您要这样,您应该回显此标题:“ header('Content-type:text / csv'); header('Content-Disposition:attachment; filename = contacts.csv'); “ and then load csv from file in some var eg. 然后从文件中加载某些格式的csv,例如。 "$csv", and then Just echo content, something like: “ $ csv”,然后仅回显内容,例如:

<?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. 如果该调用是ajax调用,并且您在调用标头之前回显了一些字符。

Just mak sure the is no character before 只是确保之前没有字符

You can add: 你可以加:

ob_start() at start ob_start()在开始时

and

ob_flush() at end ob_flush()结尾

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. ob_gzhandler()旨在用作ob_start()的回调函数,以帮助促进将gz编码的数据发送到支持压缩网页的Web浏览器。

You see: 你看:

PHP net ob_start PHP网ob_start

and

PHP net ob_gzhandler PHP网ob_gzhandler

暂无
暂无

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

相关问题 PHP - 无法修改标头信息 - 标头已由(输出开始于 - PHP - cannot modify header information - headers already sent by (output started at 为什么收到警告:无法修改标头信息-标头已经发送过(输出从...开始)? - Why am I getting Warning: Cannot modify header information - headers already sent by (output started at …)? PHP 重现警告“无法修改 header 信息 - 标头已由(输出开始于...”与 XAMPP 8..1.1 - PHP reproduce warning "Cannot modify header information - headers already sent by (output started at... " with XAMPP 8..1.1 警告:无法修改已发送的标头信息标头(输出始于(/themes/kaboodle/functions/admin-setup.php - Warning:Cannot modify header information-headers already sent by (output started at (/themes/kaboodle/functions/admin-setup.php 如何删除警告无法修改标头信息-标头已发送 - How to remove warning Cannot modify header information - headers already sent by 警告:无法修改标头信息-标头已发送 - Warning: Cannot modify header information - headers already sent by 警告:无法修改标题信息 - 标题已在 wordpress 中发送 - Warning: Cannot modify header information - headers already sent by in wordpress 无法修改输出已发送的标题信息标题 - Cannot modify header information headers already sent by output 无法修改标题信息-标题已发送 - Cannot modify Header Information - headers already sent 无法修改标头信息 - 标头已发送 - Cannot modify header information - headers already sent
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM