如何导出到XML文件而不是在屏幕上打印?

时间:2011-07-29 21:07:51

标签: php xml

如何编辑我在下面使用的代码来保存生成的文件而不是在屏幕上打印?

<?php
header("Content-type: text/xml");

$linkID = mysql_connect($host, $user, $pass) or die("Could not connect to host.");
mysql_select_db($database, $linkID) or die("Could not find database.");

$query = "";
$resultID = mysql_query($query, $linkID) or die("Data not found.");

$xml_output = "<?xml version=\"1.0\"?>\n";
$xml_output .= "<products>\n";

for($x = 0 ; $x < mysql_num_rows($resultID) ; $x++){
    $row = mysql_fetch_assoc($resultID);

    $xml_output .= "\t<product>\n";
.
.
.
    $xml_output .= "\t</product>\n";
}

$xml_output .= "</products>";

echo $xml_output;
?> 

1 个答案:

答案 0 :(得分:3)

您可以使用“Content-Disposition”标头提供推荐的文件名,并强制Web浏览器显示保存对话框。

例如:

<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="downloaded.xml"');

// ...your other code
?>

这不能保证适用于所有服务器设置和所有浏览器,因此您可以在PHP标头功能页面上找到更多信息:http://php.net/manual/en/function.header.php

相关问题