Reputation: 9
I have a textarea, text input and a button wich sends textarea and text input values (using AJAX post method) to PHP site. Now, I want PHP to output textarea value as .txt file so that it can be download via browser.
My PHP code looks like this:
$text=trim($_POST['text']); // textarea value
$fileName=$_POST['fileName'].".txt"; // text input value
header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Content-Length: ".mb_strlen($text));
print($text);
Nothing happens, so can you help me please. Thanks. :)
Upvotes: 0
Views: 360
Reputation: 4699
Have you considered not sending the text to the server and just creating a download client-side?
This post has some useful suggestions you could try to create a download by bypassing the server completely Create a file in memory for user to download, not through server
Upvotes: 1
Reputation: 6662
Maybe you should clean the buffer
while (@ob_end_clean());
ob_start();
header("Content-Type: text/plain");
header('Content-Disposition: attachment; filename="'.$fileName.'"');
header("Content-Length: ".mb_strlen($text));
print($text);
ob_end_flush();
exit();
Upvotes: 0