如何在 PHP 中将 XML 文件保存到本地文件系统(客户端)?

发布于 2024-10-17 23:16:03 字数 557 浏览 5 评论 0原文

在我的网站中,我实现了以下功能:如果用户单击按钮,则会触发 PHP 函数,该函数生成 XML 文件(该 PHP 函数由 AJAX 调用)。一切都运行良好,但我想更改一件事:我不想在服务器计算机上创建 .XML 文件;我不想在服务器上创建 .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"); 

感谢您的帮助。

In my site, I have implemented the following functionality: If the user clicks on a button it triggers a PHP function which generates an XML file (that PHP function is called by AJAX). Everything is working well, but here's one thing I want to change: I don't want an .XML file to be created on the server machine; instead, I want the user to be prompted to save the .XML file locally. How do I do that? My PHP script currently looks like this:

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

Thanks for all the help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

假装不在乎 2024-10-24 23:16:03

每个 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?

Every web content has a header, so if you specify the header for an xml file (through the use of the header() function with the appropriate code it'll work.
This would mean doing something like this:

<?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?
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文