[英]How to save an XML file to local file system (client) in PHP?
在我的站点中,我实现了以下功能:如果用户单击按钮,它将触发一个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");
感谢您的所有帮助。
每个Web内容都有一个标头,因此,如果您为xml文件指定标头(通过将header()
函数与相应的代码配合使用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?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.