Download output instead of display it

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
MicroSun
Forum Newbie
Posts: 3
Joined: Mon Dec 12, 2005 2:48 am

Download output instead of display it

Post by MicroSun »

Hello,

I want to make a small tool which shows the actual DB table description. There is no problem with it.

But I want to allow the user to choose download the result as a text file instead of displaying it in the browser. How can I do this?

Thanks!
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

You will have to build the string, write it to a file (txt file perhaps?) then use headers to download as a file.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

You don't need to write it to a file. All that's required is the right combination of header() settings.
User avatar
nathanr
Forum Contributor
Posts: 200
Joined: Wed Jun 07, 2006 5:46 pm

Post by nathanr »

Code: Select all

header("Content-type: text/plain"); //set the content type to a txt file
    header("Content-Disposition: attachment; filename=mytabledump.txt"); //say its an attachment, thus download, and the donwload file name
    header("Pragma: public"); //resolves ie6 error where content-disposition isn't always recognised
    header("Cache-Control: max-age=0"); //don't cache it
    echo $file_contents; //output the table dump to appear in the file
This should do the trick i think, just wrote it here though so not tested!

here are some mime types for downloading (by extension) always a useful reference

hope that helps 8O
Post Reply