Generate a .txt and then force download [duplicate]
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.
精彩评论