【问题标题】:How can I export to an XML file instead of printing it on screen?如何导出到 XML 文件而不是在屏幕上打印?
【发布时间】:2011-07-29 21:07:50
【问题描述】:

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

<?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;
?> 

【问题讨论】:

标签: php xml


【解决方案1】:

您可以使用“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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-24
    • 2013-05-28
    相关资源
    最近更新 更多