下载标题以某种方式插入18行的空格 [英] Download headers somehow inserting 18 lines of whitespace

查看:136
本文介绍了下载标题以某种方式插入18行的空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图提供一个动态生成的csv文件。出于某种原因,当我获取文件时,数据前有18个空行。我在定义的头文件和发送的csv数据之间没有空间。如果我将数据写入服务器上的文件,它不会得到这些空行。但是,如果我编写文件并尝试将其提供给用户,则空行会回来。所以我想知道如果也许我已经弄乱了标题,或者如果可能有另一个问题,我没有想到:

  function generate_csv($ source_type,$ include_unpublished = FALSE){

//从DB
...检索数据

//启动标题
$ csv_name =$ source_type- $ data_set-csv_。日期('Y-m-d')。 .csv文件;
header('Content-Type:text / x-comma-separated-values');
header('Cache-Control:must-revalidate,post-check = 0,pre-check = 0');
header('Cache-Control:private',false); //某些浏览器需要
header('Content-Disposition:attachment; filename =''。$ csv_name。'');

//发送csv数据
打印$ csv_data;
$ b $ //结束函数

免责声明:我在< https://drupal.stackexchange.com/questions/27649/extra-empty -rows-when-serving-csv-file ,但它似乎并不是drupal特有的,并且在那里没有太多想法。

解决方案

它必须是包含的文件的问题。在php结束标记?>> 后,每个空格都会有多个换行符被发送到浏览器。 最佳解决方案是在每个php文件中摆脱这个结束标签。

其他选项将只删除不必要的新行,或者在提供文件之前缓冲输出并忽略它。

I am trying to serve up a dynamically generated csv file. For some reason when I get the file, there are 18 empty rows preceding the data. I don't have any space between the headers I define and the csv data I'm sending. If I write the data to a file on the server, it does not get these empty rows. However, if I write the file and then try to serve it to the user, the empty lines come back. So I'm wondering if perhaps I've messed up the headers, or if perhaps there is another issue I'm not thinking of:

function generate_csv($source_type, $include_unpublished = FALSE) {

   // retrieve data from DB
   ....

   // start up headers
   $csv_name = "$source_type-$data_set-csv_" . date('Y-m-d') . '.csv';
   header('Content-Type: text/x-comma-separated-values');
   header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
   header('Cache-Control: private', false); // required for certain browser
   header('Content-Disposition: attachment; filename="' . $csv_name . '"'); 

   // send csv data
   print $csv_data;

} //end function

Disclaimer: I asked this question at https://drupal.stackexchange.com/questions/27649/extra-empty-rows-when-serving-csv-file, but it dosn't seem to be drupal-specific and there weren't many ideas coming up over there..

解决方案

It must be problem with files that you are including. Every whitespace more than one newline after php closing tag ?> is sent to the browser.

Best solution is to get rid of this closing tags in every php file.

Other option will be to remove only unnecessary new lines from them or to bufer output and disregard it before serving file.

这篇关于下载标题以某种方式插入18行的空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆