【问题标题】:On xml file download, a new line is added to start of the file在 xml 文件下载时,会在文件开头添加一个新行
【发布时间】:2019-03-06 14:22:20
【问题描述】:

我成功生成了一个用于谷歌地球的 xml 文件。 当我打开文件时,它看起来像这样。文件从第 1 行开始Xml generated

然后我生成一个链接供用户下载文件。这些文件现在从第 2 行开始。 File download from the link.

我不明白为什么会这样。 这是我用来将文件发送给用户的代码。

     $target_file =  "../files/google.xml";
      $fh = fopen($target_file, 'r');
      $file_stream = new Stream($fh);

    return $response ->withHeader('Content-Type', 'application/xml; charset=utf-8')
                  ->withHeader('Content-Type', 'application/force-download')
                ->withHeader('Content-Type', 'application/download')
                ->withHeader('Content-Description', 'File Transfer')
                ->withHeader('Content-Disposition', 'attachment; filename="' . basename($target_file) . '"')
                ->withHeader('Expires', '0')
                ->withHeader('Cache-Control', 'must-revalidate, post-check=0, pre-check=0')
                ->withHeader('Pragma', 'public')
               ->withBody($file_stream);

有人可以告诉我为什么会这样吗?

【问题讨论】:

    标签: php xml slim kml


    【解决方案1】:

    文件中<?php 标记之前的任何内容都会输出到浏览器。检查文件是否在开头没有任何空格 - 换行符、BOM 标记、Unicode 不间断空格,什么都没有。任何 include()d 或 require()d 文件也是如此。

    【讨论】:

    • 谢谢..我还没有任何空格或换行符,但我仔细检查了这一点。我正在为我的模板使用 twig,我不知道它是否默认添加换行符,所以现在,仍然仔细检查是否有任何空格或换行符
    • php 之前的换行符在调用所有控制器的文件中。我永远不会发现你没有精确任何包含或必需的文件。非常感谢。
    • 不客气。我自己不止一次被这个虫子咬过;)
    猜你喜欢
    • 1970-01-01
    • 2017-09-11
    • 2013-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多