开发者

Generate a .txt and then force download [duplicate]

开发者 https://www.devze.com 2023-02-04 20:11 出处:网络
开发者_开发问答This question already has answers here: How to Automatically Start a Download in PHP?
开发者_开发问答This question already has answers here: How to Automatically Start a Download in PHP? (5 answers) Closed 7 years ago.

I have a php function for getting info out of my database. When they go to 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.

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


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

0

精彩评论

暂无评论...
验证码 换一张
取 消