【发布时间】:2016-03-02 16:09:56
【问题描述】:
我是 php 新手,我对你有疑问。 现在,我正在创建一个我想要使用的 php 脚本:
$file = fopen('php://memory','w');
然后,我使用 fputcsv 在其中添加几个数组来填充此文档。 在最后部分,我说我要制作可下载的链接
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="service.csv"');
我的方法是这样的:
public function exportToCsv($Id){
$file = fopen('php://memory','w');
$someDetails = $this->some_model->getSomeDetail($Id);
$details = array(
array("Subject", "Start Date", "Start Time", "End Date", "End Time", "Where"),
array("test subject","test date","test time","test end date","etc..."));
foreach ($details as $detail) {
fputcsv($file, $detail);
}
fseek($file,0);
header('Content-Type: application/csv');
header('Content-Disposition: attachment; filename="file.csv"');
fpassthru($file);}
当我的浏览器下载这个文件并打开它时,一切都很好,但是在文件的开头,有一个空行,我不明白为什么会发生这种情况以及如何删除这一行。
感谢描述。
马雷克
【问题讨论】:
-
php标签前后有空格/空行吗? ()
-
不,我使用了 CodeIgniter 框架,这段代码是在控制器中编写的。未找到空行/空格。当我打开创建的文档时,第一行是空白的,其他行是填充的。而且我忘了提及..所有这一切仅在我使用路径“php://memory”或输出时才发生,...当我写像“C:/path/to/folder”这样的路径时,一切都很好,没有空行created ... only "php://memory" 在开头创建空行
-
尝试添加 ob_end_clean();就在你的 fpassthru($file);
-
多么棒的建议!有用!我想吻你:D 谢谢
-
没问题,很高兴它成功了!我会将我的评论改写为答案,以便您接受:)