31

Ultimate goal: I want to create a webpage where a user can enter information in forms. With that information I want to create a html file (below called test-download.html) by inserting the information given into a template and then force a download. Since I want to demonstrate this at an upcoming workshop where people will be using this at the "same time" I would like to not save the file on the server and just force the download.

So far: I have this in my html file (test.html):

<form action="test.php" method="post">
To file: <input type="text" name="tofile" />
<input type="submit" />
</form>

and this in my test.php:

<?php
$filename = 'test-download.html';
$htmlcode1 = "<HTML> \n <BODY>";
$htmlcode2 = "</BODY> \n <HTML>";
$somecontent = $htmlcode1.$_POST["tofile"].$htmlcode2;
!$handle = fopen($filename, 'w');
fwrite($handle, $somecontent);
fclose($handle);


header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Length: ". filesize("$filename").";");
header("Content-Disposition: attachment; filename=$filename");
header("Content-Type: application/octet-stream; "); 
header("Content-Transfer-Encoding: binary");

readfile($filename);

?>

This overwrites the file test-download.html file and forces a download.

Question: How can I do this without messing with a file (the test-download.html) on the server?

Thomas
  • 1,085
  • 5
  • 19
  • 33

2 Answers2

35

Instead of saving it to a file, just echo it after you send the headers.

alex
  • 479,566
  • 201
  • 878
  • 984
  • @Thomas Yep, do that after the headers, and remove the saving and reading from file code. – alex Apr 06 '11 at 01:07
  • I just tried it and it seems to work! A bit of a wow experience there. Thanks again. – Thomas Apr 06 '11 at 01:15
  • 3
    @arrowill12 The only *code* in my answer is `echo`, I'm not sure how that can be corrected. – alex Dec 07 '12 at 22:59
  • Thanks for this Thomas & Alex. I also changed the Content-Length to reflect the size of the content string since there is no file anymore :) i.e. header("Content-Length: ". strlen($somecontent).";"); – Ryan H. Dec 23 '14 at 21:46
20

Realize that nearly every time a PHP script responds to a request, it's "generating a file" that's downloaded by the browser. Anything you echo, print, printf, or otherwise put out to standard output is the contents of that "file".

All you have to do is tell the browser that the "file" should be handled differently -- and the headers you're outputting should already do that. Once the headers are sent, anything you print out becomes contents of the download.

cHao
  • 84,970
  • 20
  • 145
  • 172
  • Thank you for the explanation. I didn't know that one doesn't have to save the file but can just pass it on for download. – Thomas Apr 06 '11 at 01:23
  • 2
    @Thomas: Yeah, it's a bit of a mind-blower when you first realize it. But a browser basically just says "GET /some/url" and gets back a bunch of bytes (the response) and some metadata (the headers). It doesn't know or care how they were generated, or where they were stored, or indeed whether they were stored anywhere at all. – cHao Apr 06 '11 at 01:27
  • @cHao My Mind + Your info = Blown! – usumoio Jun 13 '13 at 15:28