Search code examples
phpxmlslimkml

On xml file download, a new line is added to start of the file


I successfully generated a xml file to be use for google earth. It looks like this when I open the file. The file starts at line 1 Xml generated

I then generate a link for users to download the file. The files now starts at line 2. File download from the link.

I can't figure why this is happening. Here's the code I used to send the file to the user.

     $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);

Can someone please pinpoint me to why this is happening ?


Solution

  • Whatever is in your file before the <?php tag, that is output to the browser. Check that the file doesn't have any whitespace at the beginning - line breaks, BOM marks, Unicode non-breaking spaces, nothing. The same goes for any include()d or require()d files.