3

So I have a form that users complete, and they are sent to a php page that sends all the variables via $_POST and then writes the output to a txt file. - that works just fine. My problem is I am trying to force the user to download it on that same page that creates the file, after the file is created and I can't seem to get it to work, nothing happens besides the file being created....

Here are the basics of what i have:

EDIT: Found the problem - I am getting a fatal error on the line with my fclose... any ideas what's wrong there?

$myfile="c-form" . date('m-d-Y_hia').'.txt';
$fileHandle = fopen($myfile, 'w');
//write stuff to file
$fclose($fileHandle);
//file is now closed
//now force user to download file that was just made
header('Content-disposition: attachment; filename=$myfile');
header('Content-type: text/plain');
  • okay, so where in your code do you actually send the file content back as part of the POST response? – Mike 'Pomax' Kamermans Dec 17 '13 at 00:39
  • Why do write stuff to the file instead of just outputting it after the headers? The `attachment; filename=...` won't actually transfer it by itself. – mario Dec 17 '13 at 00:41
  • Are you talking about after the user submits the form and the content is written to a file, or after the file is closed and it should be downloaded? –  Dec 17 '13 at 00:41
  • @mario how exactly do I transfer the file then? –  Dec 17 '13 at 00:45
  • Your first sentence is somewhat confusing. Is the data being POSTed to or from your server? – virmaior Dec 17 '13 at 01:00
  • Data is posted to the server when the form is submitted, then I grab all the variables from the POST array and use them to create my text file –  Dec 17 '13 at 17:25

2 Answers2

3

Try something like this:

$myfilename="c-form".date('m-d-Y_hia').'.txt';
// collect the data to the be returned to the user, no need to save to disk
// unless you really want to, if so, use file_put_contents()
$dataForFile="test data to be returned to user in a file";

header('Content-type: application/x-download');
header('Content-Disposition: attachment; filename="'.$myfilename.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.strlen($dataForFile));
set_time_limit(0);
echo $dataForFile;
exit;

There is no need to use fopen() and fclose(), file_get_contents() and file_put_contents() will do all that for you, and has done for years now.

Tigger
  • 8,980
  • 5
  • 36
  • 40
  • 1
    I've never used file_put_contents() or file_get_contents() before, how would I implement those in place of fopen, and fclose? I still need it to be saved to the disk. –  Dec 17 '13 at 17:32
  • @John - The PHP documentation for [file_put_contents()](http://php.net/file_put_contents) and [file_get_contents()](http://php.net/file_get_contents) does a much better job than what I could here. – Tigger Dec 17 '13 at 22:03
  • I read up on file_put_contents, and it worked for me since I was getting a fatal error on fclose, so I'm going to mark your solution as the answer. –  Dec 18 '13 at 17:45
3

You need to use double quotes in the line below:

header("Content-disposition: attachment; filename=$myfile");
Şafak Gezer
  • 3,928
  • 3
  • 47
  • 49
  • 1
    For more information on the [differences between quotes in PHP](http://stackoverflow.com/questions/3446216/what-is-the-difference-between-single-quoted-and-double-quoted-strings-in-php) – Simon Robb Dec 17 '13 at 01:26