douqiu0351 2015-09-25 19:51
浏览 82

无法使用PHP中的header()下载csv字符串[重复]

This question already has an answer here:

I read a whole bunch of articles in SO and the internet and tried all of them but I am still unable to create a CSV download functionality in PHP.

Following is my code:

$csvData = @$_POST['csv_data'];
if(trim($csvData))
{
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header('Content-Description: File Transfer');
    header("Content-type: text/csv");
    header("Content-Disposition: attachment; filename=data.csv");
    header("Expires: 0");
    header("Pragma: public");
    echo $csvData;
    exit;   
}

Instead of showing the file save dialog, this keeps printing the CSV data into my browser :( What am I doing wrong here? Any help is much appreciated..

Additional Edit: I am posting this data into my script, which immediately takes this data and tries to download. My script is an include inside another file, will that be a problem? I enabled error_reporting and found that I am getting header already modified error...

</div>
  • 写回答

1条回答 默认 最新

  • duanbenzan4050 2015-09-25 20:00
    关注

    In HTTP, headers are sent before any text output.

    For that reason, PHP will close your response header if you output any text.

    Make sure that you hold any text output before your header modification, for example using the OB-cache.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分