What has been posted below is spot on for getting it to output the file. What it is doing though is outputting the string data followed by the rest of the forms HTML. Is there any way to stop what is put into the file and what is just displayed to the browser.
exit() and all works fine. Thanks!
I’ve been looking around and seen a few things similar but not quite fully got the grasp on what I need to do to achieve the task fully.
At the moment I have a form that a user supplies some details. On submit it is posted back to itself and the POST variables processed. I have a premade HTML web template for the information to be placed into which works fine and dandy just doing a str_replace.
What I am trying to do now is export that as a download to the user in a plain text document. So the end result being the user clicks submit on the form and they then have a download popup open with the modified webpage as a .txt file.
As far as I understand I need to do something using HTTPs headers functionality. What exactly though to achieve what I want I’m not sure. I only want the file to be available once, but I assume it has to be stored somewhere initally for the user to download which will then need cleaing up after manually?
Any help or points would be great! Thanks.
Check out this SO question’s accepted solution. Substitute your own filename for
basename($File) and change filesize($File) to
strlen($your_string). (You may want to use mb_strlen just in case the string contains multibyte characters.)
No need to store it anywhere. Just output the content with the appropriate content type.
<?php header('Content-type: text/plain'); ?>Hello, world.
Add content-disposition if you wish to trigger a download prompt.
header('Content-Disposition: attachment; filename="default-filename.txt"');
Use below code to generate files on fly..
<? //Generate text file on the fly header("Content-type: text/plain"); header("Content-Disposition: attachment; filename=savethis.txt"); // do your Db stuff here to get the content into $content print "This is some text...\n"; print $content; ?>
<?php header('Content-type: text/plain'); header('Content-Disposition: attachment; filename="<name for the created file>"'); /* assign file content to a PHP Variable $content */ echo $content; ?>