簡體   English   中英

如何使用php創建XML文件並提示下載?

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

我正在使用domdocument創建一個xml文件:

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

當我單擊此文件的鏈接時,它只是將其顯示為xml文件。 我如何提示下載呢? 此外,如何提示我將其下載為“ backup_11_7_09.xml”(插入今天的日期並將其作為xml),而不是php文件的真實名稱“ backup.php”

在回顯之前,將Content-Disposition標頭設置為附件:

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

當然,您可以使用strftime()格式化myfile.xml以獲得文件名中的格式化日期:

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

    echo $dom->saveXML();
?>
<?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
?>

使用content-disposition標頭。

這應該工作:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM