[英]PHP File creation and download on the fly
Hey guys I was wondering how I could download a file that is generated on the fly by PHP. 大家好,我想知道如何下载PHP即时生成的文件。 The file I want to download would be an XML file.
我要下载的文件将是XML文件。 At the moment all my code does is create a long string with all of the data that is to put in the file, it then simply writes the string to a file and saves it with a .XML extension.
目前,我所有的代码都是使用要放入文件中的所有数据创建一个长字符串,然后将其简单地将该字符串写入文件并以.XML扩展名保存。 This is currently working in my local machine using a copy of the website, it won't work on the web server though due to read/write permissions.
目前,这在使用网站副本的本地计算机上有效,但由于具有读/写权限,因此无法在Web服务器上工作。
So is there a way to generate a file in the fly to be immediately downloaded without storing it on the web server? 那么,有没有一种方法可以立即生成文件以立即下载而不将其存储在Web服务器上?
If the same script is generating the file, you could echo
or print
the contents on to the page and use header
to force download. 如果使用相同的脚本生成文件,则可以在页面上
echo
或print
内容,并使用header
强制下载。
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="file.xml"');
echo $XMLString;
?>
And if you have a different file for downloading the file, just use file_get_contents
and output the file data! 如果您有其他文件要下载,请使用
file_get_contents
并输出文件数据!
That should do you :) 那应该做你:)
Just give these two things on the top of the document: 只需在文档顶部提供以下两点即可:
<?php
header('Content-type: text/xml');
header('Content-Disposition: attachment; filename="myxml.xml"');
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.