Reputation: 11602
My problem is that I have a string that gets generated from database queries, timestamps, and other random things and is always different so I don't have a "file" to download. How can I use this generated string to be downloaded as a .txt
file in the browser for the user? I know that you can download a file to the browser using headers so maybe something like this:
$result = 'Generated string that is different every time. 20121107160322';
header("Content-Type: text/plain");
header("Content-Disposition: attachment; content=$result");
header("Content-Length: " . strlen($result));
Obviously this doesn't work, but I don't even know where to start.
Upvotes: 3
Views: 7807
Reputation: 50573
You almost got it, you just lack to actually output the string to the browser, and also give it a filename that the browser can use to prompt the user, so do like this:
$result = 'Generated string that is different every time. 20121107160322';
$filename = 'result_file.txt';
header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$filename.'"');
header("Content-Length: " . strlen($result));
echo $result;
exit;
Upvotes: 5