Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Downloading data from textarea 1

Status
Not open for further replies.

cian

Technical User
Oct 11, 2001
1,383
Hi guys,

Dilema: I have text data contained in a textarea which is stored in a database. It's basically a "scratch pad" type thing.
I want to allow users to download the data and am trying to figure out the easiest way.
Should I call the data from the database and write it into a text file? How then do I force the download?
(I could write to a text file at the same time as I store it in the databse but that negates the need for a database)

Any ideas? I'm stumped on where to start with this one.


<!--#sig value=''É'' url='' -->
 
I'd just accept the input and send it back to the browser.

Take a look at the PHP online manual entry on header(). There is a section on that page which talks about forcing the "Save as" dialog in a browser.

Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Excellent, thank you sleipnir214

The header() info gave me the solution.
Here's what I came up with.
At the top of the page with the download link I have the following:

if ($show == "download-notes")
{
header("Content-Type: text/richtext");
header("Content-Disposition: attachment; filename=Notes.txt");

require_once('database.php');

$note ="SELECT notes FROM notepad Where id = '1'";
$res = mysql_query($note)
or die("Query failed: $res<br>" . mysql_error());
$notes = mysql_fetch_array($res);
$notes = stripslashes($notes['notes']);
$txt = ('Notes from ').date("l dS F Y").('
Downloaded at ').date("g:i a

");
echo $txt;
echo $notes;
exit;
}

Works like a charm so far. Thanks again!

<!--#sig value=''É'' url='' -->
 
thanx, i was was looking for a similar thing for my website :)

just one thing, where can i find all the different Content-Type's??

thanx


Martin

Computing help and info:

 
To the best of my knowledge, there is no such thing as a canonical list of HTTP content-types. Every browser deals with different types differently. But the closest thing to a "must-adhere-to" list is the one at the IANA.

When forcing a download of a file, IE is notorious for paying more attention to the filename extention than the content-type. The following headers:

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment; filename=foo.xls");

will cause IE to make Excel available, simply because the Content-dispostition tag mentions a filename with a .xls extension. ("application/octect-stream" is the generic universal content-type).

Also, you can just make up your own Content-Type headers: "application/x-<something>" is an experimental content-type.



Want the best answers? Ask the best questions!

TANSTAAFL!!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top