Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show 'Save as' dialog box using PHP for text files

How can I show the 'Save as' dialog box using PHP which will ask the user to download a string as a text file? Basically I will retrieve some values from the database, and want then to be able to download a .txt file which contains this data.

like image 350
Ali Avatar asked Apr 08 '09 22:04

Ali


3 Answers

This should work:

header('Content-type: text/plain');
header('Content-disposition: attachment; filename="test.txt"');
like image 124
Emil H Avatar answered Oct 16 '22 05:10

Emil H


Just to expand on @Emil H's answer:

Using those header calls will only work in the context of a new request. You'll need to implement something that allows your script to know when it's actually sending the file as opposed to when it's displaying a form telling the user to download the file.

like image 40
Randolpho Avatar answered Oct 16 '22 05:10

Randolpho


<?
header ("Content-Type: application/download");
header ("Content-Disposition: attachment; filename=$yourfile");
header("Content-Length: " . filesize("$yourfile"));
$fp = fopen("$yourfile", "r");
fpassthru($fp);
?>
like image 20
billypostman Avatar answered Oct 16 '22 06:10

billypostman