[英]remove unwanted space in start of CSV file generated using PHP
I am generating CSV file using PHP. 我正在使用PHP生成CSV文件。 What i am doing is 我在做什么
<?php
header("Content-type: text/csv");
header("Content-Disposition: attachment; filename="myfile.csv");
header("Pragma: no-cache");
header("Expires: 0");
echo "mycontent";
exit;
It generates the CSV file with my content, but two extra spaces (tab spaces, i guess) are being added in the start of CSV content. 它使用我的内容生成CSV文件,但是在CSV内容的开头添加了两个额外的空格(我猜是制表符空格)。 I have used trim(), and other related functions to remove spaces, but it does not help. 我已经使用trim()和其他相关函数来删除空格,但这无济于事。
Sample output (with spaces): 样本输出(带空格):
first_column,second_column
first_data,second_data
but it should be: 但应该是:
first_column,second_column
first_data,second_data
Please help me how to remove these two extra spaces in the start of CSV file. 请帮助我如何删除CSV文件开头的这两个多余的空格。
I had this exact same problem and finally found a solution after many hours of searching. 我遇到了完全相同的问题,经过数小时的搜索终于找到了解决方案。 Adding: ob_end_clean() after the header fixed it... 在标头修复后添加:ob_end_clean()...
header('Content-Disposition: attachment; filename="sample.txt"');
ob_end_clean();
I had never used ob_end_clean() before but looks like it cleans up buffers and other things that seem to lead to a double whitespace at the beginning of files created for download in this way. 我以前从未使用过ob_end_clean(),但看起来它会清理缓冲区和其他东西,这些东西似乎在以这种方式创建的要下载文件的开头导致了双空格。 I only found the solution as it was part of the code in a sample not related to this problem so hopefully someone in the future will stumble across this post, fix the problem and move on with their life! 我只是找到了解决方案,因为它是与该问题无关的示例代码中的一部分,因此希望将来有人会偶然发现这篇文章,解决问题并继续他们的生活!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.