0

I have a generated xls file on my server that I would like to push to the client for download but can't seem to get it working. Here is what I have so far:

$xlsFile = 'test.xls';

header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");;
header("Content-Disposition: attachment;filename=$xlsFile"); 
header("Content-Transfer-Encoding: binary ");  
exit();

My excel file is correct and can be opened if I open it from the server but how can I push this file to the client for download?

user1216398
  • 1,890
  • 13
  • 32
  • 40
  • what's the point of th emultiple content-types? It's like saying "I'm a car, I'm a giraffe, I'm a stick of gum". Unless you're doing multi-part mime, an http request reply can only have a single mime type. – Marc B Jun 26 '12 at 15:40

4 Answers4

3

You seem to be overdoing it with your "Content-type" headers. Oh, and you need to actually send the file before exit()ing. All you need is

header("Content-Type: application/vnd.ms-excel");
header('Content-Disposition: attachment; filename="' . $xlsFile . '"');
readfile($xlsFile);
exit();
Aleks G
  • 56,435
  • 29
  • 168
  • 265
0

See this older post on the correct MIME type to send with your upload:

Setting mime type for excel document

Community
  • 1
  • 1
Jon
  • 3,230
  • 1
  • 16
  • 28
0

You just need to add one more line

$xlsFile = 'test.xls';

header("Content-Type: application/vnd.ms-excelapplication/vnd.ms-excel");
header("Content-Disposition: attachment;filename='$xlsFile'"); 
header("Content-Transfer-Encoding: binary ");  
echo file_get_contents($xlsFile);
exit();
Havelock
  • 6,913
  • 4
  • 34
  • 42
0

Sample function.

function file_download($filename, $mimetype='application/octet-stream') {
  if (file_exists($filename)) {
// send headers
    header($_SERVER["SERVER_PROTOCOL"] . ' 200 OK');
// type
    header('Content-Type: ' . $mimetype);
// date of modified       
    header('Last-Modified: ' . gmdate('r', filemtime($filename)));
    header('ETag: ' . sprintf('%x-%x-%x', fileinode($filename), filesize($filename), filemtime($filename)));
// file size
    header('Content-Length: ' . (filesize($filename)));
    header('Connection: close');
    header('Content-Disposition: attachment; filename="' . basename($filename) . '";');
// send file
    echo file_get_contents($filename);
  } else {
    header($_SERVER["SERVER_PROTOCOL"] . ' 404 Not Found');
    header('Status: 404 Not Found');
  }
  exit;
}