0

我正在使用 domdocument 创建一个 xml 文件:

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

当我单击此文件的链接时,它只是将其显示为 xml 文件。如何提示下载?此外,我如何提示将其下载为“backup_11_7_09.xml”(插入今天的日期并将其作为 xml)而不是 php 文件的真实名称“backup.php”

4

3 回答 3

11

在回显之前将Content-Disposition标题设置为附件:

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

当然,您可以格式化myfile.xmlusingstrftime()以获取文件名中的格式化日期:

<?
    $name = strftime('backup_%m_%d_%Y.xml');
    header('Content-Disposition: attachment;filename=' . $name);
    header('Content-Type: text/xml');

    echo $dom->saveXML();
?>
于 2009-11-07T17:44:42.617 回答
3

这应该有效:

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

如果您使用会话,请使用以下设置来防止 IE6 出现问题:

session_cache_limiter("must-revalidate");
session_start();
于 2009-11-07T17:46:00.957 回答
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
?>

使用内容处置标头。

于 2009-11-07T17:45:34.720 回答