2011-02-15 132 views
1

在我的网站中,我实现了以下功能:如果用户点击一个按钮,它将触发一个PHP函数,该函数会生成一个XML文件(该PHP函数由AJAX调用)。一切运行良好,但我想改变一件事:我不希望在服务器计算机上创建一个.XML文件;相反,我想让用户在本地保存.XML文件。我怎么做?我的PHP脚本目前看起来像这样:如何在PHP中将XML文件保存到本地文件系统(客户端)?

$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"); 

感谢您的所有帮助。

+1

这个问题在这里已经被问过很多次了,这里有一个更好的问答http://stackoverflow.com/questions/1465573/forcing-to-download-a-file-using-php – Endophage 2011-02-15 21:07:38

+0

@Endophage:感谢我似乎并没有觉得它特别有用,因为我正在使用AJAX,这是我遇到的主要问题 - 我的页面部分填充了xml内容;而我需要保存文件对话框。 ? 谢谢。 – Boris 2011-02-15 21:25:12

回答

1

每个网页的内容有一个头,所以如果你指定一个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? 
相关问题