下载标题以某种方式插入18行的空格 [英] Download headers somehow inserting 18 lines of whitespace
问题描述
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屋!