6

I'm looking into methods of how to create a file from a string, this could be plain text would save as .txt and php as .php etc but I was hoping to get some insight into it

**I found this code

$file = 'people.txt';
// Open the file to get existing content
$current = file_get_contents($file);
// Append a new person to the file
$current .= "John Smith\n";
// Write the contents back to the file
file_put_contents($file, $current);

But would I need to have people.txt saved on my server?

The force download part

header("Content-Disposition: attachment; filename=\"" . basename($File) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($File));
header("Connection: close");

Where would I need to house the above, I'm assuming the code is right to force download my ready made file?

hjpotter92
  • 78,589
  • 36
  • 144
  • 183
ngplayground
  • 20,365
  • 36
  • 94
  • 173
  • Short answer: No, you don't need to create a file first. Howver you're reading a file that doesn't exist. Look into `fopen($file, 'w+')` instead. After that you just need to echo your string after your force download headers. – h2ooooooo Feb 25 '13 at 09:45

1 Answers1

31

You do not need to write the string to a file in order to send it to the browser. See the following example, which will prompt your UA to attempt to download a file called "sample.txt" containing the value of $str:

<?php

$str = "Some pseudo-random
text spanning
multiple lines";

header('Content-Disposition: attachment; filename="sample.txt"');
header('Content-Type: text/plain'); # Don't use application/force-download - it's not a real MIME type, and the Content-Disposition header is sufficient
header('Content-Length: ' . strlen($str));
header('Connection: close');


echo $str;
TML
  • 12,813
  • 3
  • 38
  • 45
  • And how do I stop the download. I.e. if my program sends a file and later needs to send more output to the browser? Or is that just not possible? – Buttle Butkus Feb 27 '15 at 22:16
  • 1
    HTTP provides no facility for this today that I know of. – TML Feb 27 '15 at 23:03
  • Suppose this download code is executed before other code that sends output to the browser. How to prevent that further output from being put into the download file? Some answers say to add `exit();` but that is not feasible if you have some logging code or other things running that you don't want to cut off. – Buttle Butkus Feb 27 '15 at 23:05
  • I'm not clear on what your question is, but it definitely seems like it should be its own SO question and not buried in the comments of an entry that has been marked as 'Not a real question' for 2+ years... – TML Mar 01 '15 at 06:43
  • That sounds like a lot of work. – Buttle Butkus Mar 01 '15 at 10:32
  • This code REALY helps me ! ! Thanks! and if I may asc, when I start the download,**the final file becomes with a lot of HTML code** (that I do not whant !!) ... this extra data is from that page I put this code in... please, how to sove this problem? and put this trash away? – Roberval Sena 山本 Jun 10 '16 at 01:58
  • 1
    Answer is pretty simple - do not put this PHP code at the top of a file with a bunch of HTML in it. Make this a stand-alone PHP script that echo's the downloadable content and then stop processing. – TML Jun 10 '16 at 09:40