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
php force download xml
提问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
Send a content-disposition attachmentheader.
发送内容处置附件标头。
回答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");

