Here's the code for generating a text file on the file for user to download.
Make sure you don't have any output before this code,
otherwise it will be shown on the downloaded file also.
<php? //Generate text file on the fly
header("Content-type: text/plain");
header("Content-Disposition: attachment; filename=test.txt");
$str="test";
echo $str;
?>