PHP文件创建和动态下载 [英] PHP File creation and download on the fly

查看:99
本文介绍了PHP文件创建和动态下载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嘿伙计们,我想知道如何下载PHP动态生成的文件。我想下载的文件是一个XML文件。目前我的所有代码都是创建一个包含所有要放入文件的数据的长字符串,然后只需将字符串写入文件并使用.XML扩展名保存。这当前在我的本地机器上使用该网站的副本,但由于读/写权限,它将无法在Web服务器上运行。

Hey guys I was wondering how I could download a file that is generated on the fly by PHP. The file I want to download would be an XML file. 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. 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?

推荐答案

如果同一个脚本生成文件,你可以 echo 打印页面上的内容并使用标题强制下载。

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.

<?php
    header('Content-type: text/xml');
    header('Content-Disposition: attachment; filename="file.xml"');

    echo $XMLString;
?>

如果你有一个不同的文件来下载文件,只需使用 file_get_contents 并输出文件数据!

And if you have a different file for downloading the file, just use file_get_contents and output the file data!

那应该是你:)

这篇关于PHP文件创建和动态下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆