生成.txt然后强制下载

wil*_*ium 9 php database file download

我有一个PHP函数,用于从我的数据库中获取信息.当他们去http://example.com/test/download时

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

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

dav*_*ink 26

您可以沿着这些行链接到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.";
?>
Run Code Online (Sandbox Code Playgroud)

当然,传递适当的帖子或获得args,以你喜欢的方式控制它.