Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 723
willium Avatar asked Jan 17 '11 01:01

willium


1 Answers

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.

like image 179
david van brink Avatar answered Oct 19 '22 23:10

david van brink