如何使用php创建XML文件并提示下载? [英] How do I create an XML file with php and have it prompt to download?
问题描述
我正在使用domdocument创建xml文件:
I'm using domdocument to create an xml file:
$dom = new DOMDocument('1.0', 'iso-8859-1');
echo $dom->saveXML();
当我单击此文件的链接时,它只是将其显示为xml文件。我如何提示下载呢?此外,如何提示我将其下载为 backup_11_7_09.xml(插入今天的日期并将其作为xml),而不是php文件的真实名称 backup.php
When I click the link to this file, it simply displays it as an xml file. How do I prompt to download instead? Furthermore, how can I prompt to download it as 'backup_11_7_09.xml' (insert todays date and put it as xml) instead of the php file's real name which is 'backup.php'
推荐答案
在回显之前将 Content-Disposition
标头设置为附件:
Set the Content-Disposition
header as an attachment prior to your echo:
<? header('Content-Disposition: attachment;filename=myfile.xml'); ?>
当然,您可以格式化 myfile.xml
使用 strftime()
以获取文件名中的格式化日期:
Of course, you can format myfile.xml
using strftime()
to get a formatted date in the file name:
<?
$name = strftime('backup_%m_%d_%Y.xml');
header('Content-Disposition: attachment;filename=' . $name);
header('Content-Type: text/xml');
echo $dom->saveXML();
?>
这篇关于如何使用php创建XML文件并提示下载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!