5
$file = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($file, "lorem ipsum");
fclose($file);

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

File is created/open and writen but it is not downloaded.

Any help?

4 Answers4

16

The correct way to do it would be:

<?php

$file = "test.txt";
$txt = fopen($file, "w") or die("Unable to open file!");
fwrite($txt, "lorem ipsum");
fclose($txt);

header('Content-Description: File Transfer');
header('Content-Disposition: attachment; filename='.basename($file));
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header('Content-Length: ' . filesize($file));
header("Content-Type: text/plain");
readfile($file);

?>
icecub
  • 8,615
  • 6
  • 41
  • 70
  • @puerto Verify that you copied it correctly. I would never post an answer that I didn't test myself first. It works perfectly fine :) – icecub Sep 19 '18 at 16:59
  • @puerto On a side note: Keep in mind that you cannot use this in other code where there's output before the headers. And make sure your editor doesn't save the code with a [BOM](https://en.wikipedia.org/wiki/Byte_order_mark) – icecub Sep 19 '18 at 17:04
  • I create a new file, with only your code and it works. Clcking on a button and triggering this via ajax - doesn't work. So should I have a special php page just for download? –  Sep 19 '18 at 17:07
  • @puerto Yes. Ajax isn't ment to be used for file downloads. You could just make a popup for the download page. It'll automaticly close after the download started. – icecub Sep 19 '18 at 17:18
1

I think you only need content for the download process

<?PHP

//config
$namefile = "test.txt";
$content = "lorem ipsum";

//save file
$file = fopen($namefile, "w") or die("Unable to open file!");
fwrite($file, $content);
fclose($file);

//header download
header("Content-Disposition: attachment; filename=\"" . $namefile . "\"");
header("Content-Type: application/force-download");
header('Expires: 0');
header('Cache-Control: must-revalidate');
header('Pragma: public');
header("Content-Type: text/plain");

echo $content;

thank you.

BobbyRH
  • 23
  • 7
0

You need to output something in order to let the user download a file. Try this code:

$file = fopen("test.txt", "w") or die("Unable to open file!");
fwrite($file, "lorem ipsum");
fclose($file);

header('Content-Description: File Transfer');
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="FILENAME"');
header('Content-Transfer-Encoding: binary');
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Pragma: public');
header('Content-Length: test.txt');
ob_clean();
flush();
readfile('text.txt');
exit();

Change FILENAME with the name of the file you want the user to download.

Alberto
  • 674
  • 13
  • 25
0

After you created a file, you should output it's contents to browser, for example with fpassthru, as you use file handler:

$path = "test.txt";
$file = fopen($path, "w") or die("Unable to open file!");
fwrite($file, "lorem ipsum");
//fclose($file); // do not close the file
rewind($file);   // reset file pointer so as output file from the beginning

// `basename` and `filesize` accept path to file, not file descriptor
header("Content-Disposition: attachment; filename=\"" . basename($path) . "\"");
header("Content-Type: application/force-download");
header("Content-Length: " . filesize($path));
header("Connection: close");
fpassthru($file);
exit();
u_mulder
  • 54,101
  • 5
  • 48
  • 64