Send a text file back to a user

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
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Send a text file back to a user

Post by webcan »

Can anyone tell me how I can adjust the MIME-headers (I guess?) to have a PHP script generate a text file that would prompt the user to open/save the file in Internet Explorer.

(Similar to the way you download a file of transactions from your bank I guess).

Thanks,
Peter.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

This is how I used to do:

Code: Select all

<?php
header("Content-Type: text/download");
echo 'test';
?>
The file has .txt extension and the .htaccess on the text file directory with this line:

Code: Select all

AddType application/x-httpd-php .txt
Hope it helps,
Scorphus.
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

OK great -

Do you know if there is a way I can specify a default file name if they click 'save'? It seems to use the PHP script's name.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Hi webcan,

We at DevNetwork are very happy to help people, but you gotta remember that Google is yet your best friend and you must learn to use it! It saves you and we a lot of time!

Come with me: what do you have in your hands? You can easily figure out words like 'header', 'Content-Type', 'download', sure you can. So try Google first: [google]header Content-Type download[/google] and see what you get in one of the 10 first returned links, something like this:

Code: Select all

<?php
$fileName = "list.txt";
header("Content-Type: text/download");
header("Content-length: " . filesize($fileName));
header("Content-Disposition: attachment; filename = " . basename($fileName));
readfile("$fileName");
?>
With best regards,
Scorphus.
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

Thanks for the help.
I usually try to search on the site first and look in a couple PHP books I have since that would yield an immediate solution.
Post Reply