Page 1 of 1

Send a text file back to a user

Posted: Sat May 29, 2004 2:46 pm
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.

Posted: Sat May 29, 2004 6:54 pm
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.

Posted: Mon May 31, 2004 2:20 pm
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.

Posted: Mon May 31, 2004 4:14 pm
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.

Posted: Tue Jun 01, 2004 12:47 pm
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.