1

在我的站点中,我实现了以下功能:如果用户单击一个按钮,它将触发一个 PHP 函数,该函数生成一个 XML 文件(该 PHP 函数由 AJAX 调用)。一切运行良好,但我想改变一件事:我不想在服务器机器上创建 .XML 文件;相反,我希望提示用户在本地保存 .XML 文件。我怎么做?我的 PHP 脚本目前如下所示:

$xml = new DOMDocument("1.0", "UTF-8");
$rootElement = $xml->appendChild($xml->createElement("SomeNodeName"));
...

// the following doesn't really work - xml doesn't get formatted :)
$xml->formatOutput = true; 

// this creates the actual file and places it on server. that's not what i need
$xml->save("MyXMLfile.xml"); 

感谢所有的帮助。

4

1 回答 1

1

每个 Web 内容都有一个标题,因此如果您为 xml 文件指定标题(通过使用header()带有适当代码的函数,它将起作用。这意味着执行以下操作:

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

// set the filename
header('Content-Disposition: attachment; filename="pwet.xml"');

// echo the content here
echo $xml; // simply like this, maybe?
于 2011-02-15T21:05:52.123 回答