php php强制下载xml

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2696500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:23:14  来源:igfitidea点击:

php force download xml

phpxmldownload

提问by ngreenwood6

I am creating an xml file on the fly. When a user generates this file I want it to open up a download file dialog with the content that was generated. There is no actual file because it is just generated through php. Any ideas on how to do this?

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

回答by Hari Gudigundla

This is what worked for me. In readfile('newfile.xml');make sure to give the path of the file correctly. This php page is called from an html page with anchor tag which says - download:

这对我有用。在readfile('newfile.xml');务必正确给文件的路径。这个 php 页面是从一个带有锚标签的 html 页面调用的,它说 - 下载:

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

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

来源:如何强制浏览器将文件下载到磁盘而不是播放或显示它?

回答by Quentin

回答by Milind Morey

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