php force下载xml

时间:2010-04-23 05:55:10

标签: php xml download

我正在动态创建一个xml文件。当用户生成此文件时,我希望它打开包含生成内容的下载文件对话框。没有实际的文件,因为它只是通过php生成的。关于如何做到这一点的任何想法?

3 个答案:

答案 0 :(得分:14)

这对我有用。在readfile('newfile.xml');中,请确保正确提供文件的路径。这个php页面是从带有锚标记的html页面调用的,该标记表示 - download:

<?php 
header('Content-disposition: attachment; filename="newfile.xml"');
header('Content-type: "text/xml"; charset="utf8"');
readfile('newfile.xml');
?>

来源:How do I force the browser to download a file to disk instead of playing or displaying it?

答案 1 :(得分:7)

答案 2 :(得分:0)

header('Content-disposition: attachment; filename=advertise.xml');
header ("Content-Type:text/xml"); 
//output the XML data
echo  $xml;
 // if you want to directly download then set expires time
header("Expires: 0");