Page 1 of 1

Download output instead of display it

Posted: Fri Jun 09, 2006 8:35 am
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!

Posted: Fri Jun 09, 2006 8:38 am
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.

Posted: Fri Jun 09, 2006 8:41 am
by feyd
You don't need to write it to a file. All that's required is the right combination of header() settings.

Posted: Fri Jun 09, 2006 8:45 am
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