让用户下载XML文件

时间:2023-02-06 07:42:52

I have prepared an XML string in PHP and I would like to let the user download the string in an XML file.

我在PHP中准备了一个XML字符串,我想让用户在XML文件中下载字符串。

Is it possible to offer the user the download (e.g. text.xml) without physically saving the xml file to the server?

是否可以为用户提供下载(例如text.xml)而无需将xml文件物理保存到服务器?

3 个解决方案

#1


40  

<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');

echo $xml_contents;

#2


11  

If you use some kind of output buffering as a part of your framework you must add exit() below the XML output, like that:

如果您使用某种输出缓冲作为框架的一部分,则必须在XML输出下面添加exit(),如下所示:

<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');

echo $xml_contents;
exit();

Otherwise you'll get the whole buffered page, not only the XML output.

否则,您将获得整个缓冲页面,而不仅仅是XML输出。

#3


0  

When you create the XML file first using for example php´s SimpleXMLElement you might want to clean up first to prevent that html code is in that downloaded xml file. Here is what i came up with:

当您首先使用php的SimpleXMLElement创建XML文件时,您可能需要先清理以防止该HTML文件位于该下载的xml文件中。这是我想出的:

 $xml = new SimpleXMLElement("<root/>");
 $xml->addChild("foo", "bar");

 ob_end_clean();
 header_remove();

 header("Content-type: text/xml");
 header('Content-Desposition: attachment; filename="foobar.xml"');
 echo $xml->asXML();
 exit();

This is how the downloaded "foobar.xml" file looks then:

这就是下载的“foobar.xml”文件的外观:

<?xml version="1.0"?>
<root><foo>bar</foo></root>

Testet with Chrome Version 65.0.3325.181, Firefox 59.0.1 and Microsoft Edge 41.16299.371.0

Testet使用Chrome版本65.0.3325.181,Firefox 59.0.1和Microsoft Edge 41.16299.371.0

#1


40  

<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');

echo $xml_contents;

#2


11  

If you use some kind of output buffering as a part of your framework you must add exit() below the XML output, like that:

如果您使用某种输出缓冲作为框架的一部分,则必须在XML输出下面添加exit(),如下所示:

<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="text.xml"');

echo $xml_contents;
exit();

Otherwise you'll get the whole buffered page, not only the XML output.

否则,您将获得整个缓冲页面,而不仅仅是XML输出。

#3


0  

When you create the XML file first using for example php´s SimpleXMLElement you might want to clean up first to prevent that html code is in that downloaded xml file. Here is what i came up with:

当您首先使用php的SimpleXMLElement创建XML文件时,您可能需要先清理以防止该HTML文件位于该下载的xml文件中。这是我想出的:

 $xml = new SimpleXMLElement("<root/>");
 $xml->addChild("foo", "bar");

 ob_end_clean();
 header_remove();

 header("Content-type: text/xml");
 header('Content-Desposition: attachment; filename="foobar.xml"');
 echo $xml->asXML();
 exit();

This is how the downloaded "foobar.xml" file looks then:

这就是下载的“foobar.xml”文件的外观:

<?xml version="1.0"?>
<root><foo>bar</foo></root>

Testet with Chrome Version 65.0.3325.181, Firefox 59.0.1 and Microsoft Edge 41.16299.371.0

Testet使用Chrome版本65.0.3325.181,Firefox 59.0.1和Microsoft Edge 41.16299.371.0