/    Sign up×
Community /Pin to ProfileBookmark

How to download a text file?

Hi,

Can anyone give a sample script to download a text file?

You see, I had a product table in mysql then I had a script that will read the contents of the table and write it in the text file. After writing to the text file, the script should automatically download the newly created text file from the server.

I used this code:

[code=php]header(‘location: product.txt’);[/code]

But instead of showing the download pop-up window, it will display the entire contents of the text file in the browser.

I also tried this:

[code=php]header(‘Content-Disposition: attachment; filename=product.txt’);[/code]

It shows the pup-up window and downloads the file but the problem is that the contents of the file is not the products table contents but that of the scripts running.

What will be the correct code for this?

to post a comment
PHP

5 Comments(s)

Copy linkTweet thisAlerts:
@ShrineDesignsJun 23.2005 — example[code=php]<?php
$file = 'path/to/file.txt';
header('Content-type: plain/text'); /* if that doesn't force a download use application/octet-stream */
header('Content-disposition: attachment; filename="' . rawurlencode(basename($file)) . '"');
header('Content-length: ' . filesize($file));
$fp = @fopen($file, 'rb');
$txt = @fread($fp, filesize($file));
fclose($fp);
exit($txt);
?>[/code]
Copy linkTweet thisAlerts:
@SpectreReturnsJun 23.2005 — example[code=php]<?php
$file = 'path/to/file.txt';
header('Content-type: plain/text'); /* if that doesn't force a download use application/octet-stream */
header('Content-disposition: attachment; filename="' . rawurlencode(basename($file)) . '"');
header('Content-length: ' . filesize($file));
$fp = @fopen($file, 'rb');
$txt = @fread($fp, filesize($file));
fclose($fp);
exit($txt);
?>[/code]
[/QUOTE]


The content-type should be text/plain, not plain/text
Copy linkTweet thisAlerts:
@atortegaauthorJun 23.2005 — Perfect.

Thank you very much.
Copy linkTweet thisAlerts:
@ShrineDesignsJun 23.2005 — The content-type should be text/plain, not plain/text[/QUOTE]thanks, i forgot the proper mime type lol, i knew it was something along those lines
Copy linkTweet thisAlerts:
@SpectreReturnsJun 23.2005 — Just remember: It's always classification/type
×

Success!

Help @atortega spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...