0

I have the following code in an attempt to have it so a specific file on the server is downloaded:

$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename="basename.$file);
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));

All that happens is it causes the browser to download a file, in my case "Order.txt", but its blank, it is just creating this file out of no where. It doesn't do anything with the "upload/" path portion of $file.

Thanks for any help, I find headers really confusing, why I can't just have "download ($file)" is beyond me.

Edit: I'll struggle over a problem for days, finally decide to ask here then immediately after fix it my self.

I changed the location of the file to be in the same as the PHP script, I'd still like to know how to make it work with them being separate however. I also added:

readfile($file);

If I alter with of these 2 changes it doesn't work.

Also slim lined it, new code:

$file = 'Order.txt';
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=$file");
header("Content-Type: application/zip");
header("Content-Transfer-Encoding: binary");
readfile($file);
Vereonix
  • 1,341
  • 5
  • 27
  • 54

2 Answers2

0

Try change this line:

header("Content-Disposition: attachment; filename='".basename($file)."';");
Legionar
  • 7,472
  • 2
  • 41
  • 70
0

you should print content of file to stdout after headers:

$file = 'upload/Order.txt';
header("Pragma: public", true);
header("Expires: 0"); // set expiration time
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($file));
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($file));
$handle = fopen($file, "r");        
$contents = fread($handle, filesize($file));
fclose($handle);
echo $contents;
ziollek
  • 1,973
  • 1
  • 12
  • 10