创建用于即时下载的文本文件

创建用于即时下载的文本文件

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

问题描述

更新 #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 Web 模板,用于放置信息,只需执行 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.

推荐答案

无需存储在任何地方.只需输出具有适当内容类型的内容即可.

No need to store it anywhere. Just output the content with the appropriate content type.

<?php
    header('Content-type: text/plain');
?>Hello, world.

如果您希望触发下载提示,请添加内容处置.

Add content-disposition if you wish to trigger a download prompt.

header('Content-Disposition: attachment; filename="default-filename.txt"');

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

08-06 05:49