51

Update #1

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.

Update #2

Just added exit() and all works fine. Thanks!

EOU

Hi,

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.

lethalMango
  • 4,433
  • 13
  • 54
  • 92

4 Answers4

78

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"');
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
29

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;
 ?>
Dhiral Pandya
  • 10,311
  • 4
  • 47
  • 47
  • 3
    for me it always brings the source code code of the site instead that the $content –  Dec 16 '15 at 03:34
23

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.)

Community
  • 1
  • 1
simshaun
  • 21,263
  • 1
  • 57
  • 73
1
<?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;
?>
  • 2
    This is the same as this other answer http://stackoverflow.com/a/16440501/2743307 – bibi Mar 04 '16 at 17:53
  • 1
    I encountered the same problem as @eichertc did. Adding the "php" suffix to the opening tag resolves the issue. – Chui Hui Chiu Mar 05 '16 at 20:37
  • I checked my apache logs and this was the problem and this post was very useful: http://stackoverflow.com/questions/9707693/warning-cannot-modify-header-information-headers-already-sent-by-error. I came here to delete my question because it was resolved but saw it was already deleted. – Varun Verma Apr 21 '17 at 21:15