How do I create an XML file with php and have it prompt to download?

你。 提交于 2019-12-25 01:48:20

问题


I'm using domdocument to create an xml file:

$dom = new DOMDocument('1.0', 'iso-8859-1');
echo $dom->saveXML();

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'


回答1:


Set the Content-Disposition header as an attachment prior to your echo:

<?  header('Content-Disposition: attachment;filename=myfile.xml'); ?>

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();
?>



回答2:


<?php

// We'll be outputting a PDF
header('Content-type: text/xml');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="my xml file.xml"');

// The PDF source is in original.pdf
readfile('saved.xml'); // or otherwise print your xml to the response stream
?>

Use the content-disposition header.




回答3:


This should work:

header('Content-Disposition: attachment; filename=dom.xml');
header("Content-Type: application/force-download");
header('Pragma: private');
header('Cache-control: private, must-revalidate');

$dom = new DOMDocument('1.0', 'iso-8859-1');
echo $dom->saveXML();

If your using a session, use the following settings to prevent problems with IE6:

session_cache_limiter("must-revalidate");
session_start();


来源:https://stackoverflow.com/questions/1693806/how-do-i-create-an-xml-file-with-php-and-have-it-prompt-to-download

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!