创建一个文本文件,以便即时下载 [英] Create a text file for download on-the-fly

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

问题描述

更新#1

下面发布的内容是让它输出文件。它正在做的是输出字符串数据,然后输出其余的表单HTML。有没有办法阻止放入文件的内容以及刚刚显示给浏览器的内容。

What has been posted below is spot on for getting it to output the file. What it is doing though is outputting the string data followed by the rest of the forms HTML. Is there any way to stop what is put into the file and what is just displayed to the browser.

更新#2

刚刚添加 exit(),一切正常。谢谢!

Just added exit() and all works fine. Thanks!

EOU

我一直在环顾四周,看到一些类似的东西,但并没有完全掌握我需要做些什么才能完全完成任务。

I've been looking around and seen a few things similar but not quite fully got the grasp on what I need to do to achieve the task fully.

目前我有一个用户提供一些细节的表格。在提交时,它会回发给自己并处理POST变量。我有一个预制的HTML网页模板,用于放置信息,工作正常,花花公子只做一个str_replace。

At the moment I have a form that a user supplies some details. On submit it is posted back to itself and the POST variables processed. I have a premade HTML web template for the information to be placed into which works fine and dandy just doing a str_replace.

我现在要做的是导出为在纯文本文档中下载给用户。因此,最终结果是用户点击表单上的提交,然后他们打开下载弹出窗口,修改后的网页为.txt文件。

What I am trying to do now is export that as a download to the user in a plain text document. So the end result being the user clicks submit on the form and they then have a download popup open with the modified webpage as a .txt file.

据我所知我需要使用HTTPs头功能做一些事情。究竟要实现我想要的东西我不确定。我只希望文件可用一次,但我认为它必须存储在某个地方,供用户下载,然后需要在手动后进行清理?

As far as I understand I need to do something using HTTPs headers functionality. What exactly though to achieve what I want I'm not sure. I only want the file to be available once, but I assume it has to be stored somewhere initally for the user to download which will then need cleaing up after manually?

任何帮助或积分会很棒!谢谢。

Any help or points would be great! Thanks.

推荐答案

查看这个SO问题已被接受的解决方案。用 basename($ File)替换自己的文件名,并将filesize($ File)更改为 strlen($ your_string)。 (如果字符串包含多字节字符,您可能需要使用mb_strlen。)

Check out this SO question's accepted solution. Substitute your own filename for basename($File) and change filesize($File) to strlen($your_string). (You may want to use mb_strlen just in case the string contains multibyte characters.)

这篇关于创建一个文本文件,以便即时下载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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