php 创建一个文本文件以供即时下载

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4371748/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 12:51:34  来源:igfitidea点击:

Create a text file for download on-the-fly

phphttp-headers

提问by lethalMango

Update #1

更新 #1

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.

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

Update #2

更新 #2

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

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

EOU

使用单位

Hi,

你好,

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.

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

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.

目前我有一个用户提供一些细节的表单。提交时,它会回传给自身并处理 POST 变量。我有一个预制的 HTML web 模板,用于将信息放入其中,只需执行 str_replace 即可正常工作。

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.

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

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?

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

Any help or points would be great! Thanks.

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

采纳答案by simshaun

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.)

查看此问题已接受的解决方案。替换您自己的文件名basename($File)并将 filesize($File) 更改为strlen($your_string). (您可能希望使用 mb_strlen 以防字符串包含多字节字符。)

回答by Quentin

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.

如果您希望触发下载提示,请添加 content-disposition。

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

回答by Dhiral Pandya

Use below code to generate files on fly..

使用以下代码动态生成文件..

<? //Generate text file on the fly

   header("Content-type: text/plain");
   header("Content-Disposition: attachment; filename=savethis.txt");

   // do your Db stuff here to get the content into $content
   print "This is some text...\n";
   print $content;
 ?>

回答by Chui Hui Chiu

<?php

    header('Content-type: text/plain');
    header('Content-Disposition: attachment;
            filename="<name for the created file>"');
    /*
    assign file content to a PHP Variable $content
    */
    echo $content;
?>