10

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)

willium
  • 2,048
  • 5
  • 25
  • 34

1 Answers1

27

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.

david van brink
  • 3,604
  • 1
  • 22
  • 17
  • okay, seems to work. I need to adapt to a rtf, but when I changed it to gen.rtf and text/richtext, it said the file could not be opened. – willium Jan 17 '11 at 02:17
  • Glad it worked! --- I think for rtf, you'll need to understand the precise internal structure, and output all the right bytes/format stuff. (Unless maybe there's a php lib for rtf... This SO q has a link: http://stackoverflow.com/questions/1482083/how-to-generate-rtfs-with-php) – david van brink Jan 17 '11 at 02:29
  • awesome! I'll stick with plaintext :P – willium Jan 17 '11 at 02:34
  • 1
    i just want text in CONTENT (inverted commas) not the whole html tags etc. how can i do this? – hfarazm Feb 26 '14 at 12:21