0

I have this in my php code

 header('Content-disposition: attachment; filename="'.$filename.'"');
 header('Content-type: "text/xml"; charset="utf8"');
 readfile($filename);

and my file after download

  <?xml version="1.0" encoding="UTF-8"?>
<root>

has 2 or 3 spaces in front of the document , and I can't find a solution

And this is an error when i open the file:

This page contains the following errors:

error on line 1 at column 6: XML declaration allowed only at the start of the document
Below is a rendering of the page up to the first error.

and the xml file from the server is correct

the function that writes the file:

public function toXML($filename){

    $qr = mysql_query("select * from passwords where user = '".encrypt($_SESSION['username'],$key)."'"); 

    $xml = '<?xml version="1.0" encoding="UTF-8"?>'."\n";

    $xml .= "<root>";
    $xml .= "\n";

    if (!$qr) {
        die('Invalid query: ' . mysql_error());
    }

    if(mysql_num_rows($qr)>0)
    {
       while($result_array = mysql_fetch_assoc($qr))
       {
          $xml .= '<passwords>';
          $xml .= "\n";

          foreach($result_array as $key => $value)
          {
             $xml .= "<$key>";

             $xml .= "$value";

             $xml .= "</$key>";

             $xml .= "\n";
          }

          $xml.="</passwords>\n";
       }
    }

    $xml .= '</root>';

    $fp = fopen($filename,"w");

    fputs($fp,$xml);

    fclose($fp);

    return true;

}

and in the other php file

 if(!$r->toXML($filename))
    $msg = 'Failed to export';
else{ 
    $text = file_get_contents($filename);
    $bom = pack('H*','EFBBBF');
    $text = preg_replace("/^$bom/", '', $text);
    header('Content-disposition: attachment; filename="'.$filename.'"');
    header('Content-type: "text/xml"; charset="utf8"');
    echo $text;
    exit();
}

the file from server ( the $filename file )

<?xml version="1.0" encoding="UTF-8"?>
<root>
<passwords>
<id>8</id>
<title>vbghjbhv</title>
<account>o0H0uTtsOFCYHTpiZZ2/J78W/Z+xU10nx5AUZdcUvdU=</account>
<password>2vOF9Kp2p88IbbIuHcFJeaOc1VJi40KkrAtPIEV5exg=</password>
<url>jhbvgi</url>
<user>2PTpbCnciq+dbuEcyP6v6tZxrQeXcJfRl33YfQO3aFk=</user>
<category>Business</category>
<note>ghujhvgty</note>
</passwords>
</root>
kld
  • 25
  • 3

2 Answers2

0

One solution would be to store the file in a variable, then use trim() before outputting the contents of the file.

$file = file_get_contents($filename);
echo trim($file);
jay
  • 177
  • 1
  • 12
  • same result, i still think is from the download, because before header I echo the file and the output is good, – kld Jun 01 '15 at 20:58
  • @kloudew another thing I sometimes overlook is whitespace at the beginning of my php file, before the opening tag. – jay Jun 01 '15 at 21:03
0

First, check that your file on which you are working is being saved at proper charset (UTF-8 without BOM).

Second, we do not see actual filled contents of $filename. Let's say they are in $contents variable. Probably you filled it like this:

$contents .= '
... something ';

Make sure you get rid of blank lines while populating and write it like this:

$contents .= '... something ';
DarioBB
  • 663
  • 2
  • 8
  • 29