php 生成一个.txt然后强制下载

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

Generate a .txt and then force download

phpdatabasefiledownload

提问by willium

I have a php function for getting info out of my database. When they go to http://example.com/test/download

我有一个用于从我的数据库中获取信息的 php 函数。当他们访问http://example.com/test/download

I want to create a fake test.txt (text is dynamic) and download it. It's contents should be the equivalent of executing foreach(databaseContent() as $content) { echo $content . '<br/>' }inside of it.

我想创建一个假的 test.txt(文本是动态的)并下载它。它的内容应该等同于在其中执行foreach(databaseContent() as $content) { echo $content . '<br/>' }

How can I get started on this? (Using php)

我该如何开始呢?(使用php)

回答by david van brink

You can link to a php document along these lines, which forces a download of type plain text. (Well, suggests to the browser that that should happen, at any rate.)

您可以按照这些行链接到 php 文档,这会强制下载纯文本类型。(好吧,建议浏览器无论如何都应该发生这种情况。)

<?php
header('Content-disposition: attachment; filename=gen.txt');
header('Content-type: text/plain');


echo "this is the file\n";
echo " you could generate content here, instead.";
?>

Of course, pass in appropriate post or get args, to control it the way you like.

当然,通过适当的帖子或获取参数,以您喜欢的方式控制它。