PHP: send WORD document file to download

16,415

Solution 1

From the few characters of content that you show, you're creating an OfficeOpenXML (.docx) file rather than a BIFF (.doc) file, so the content type should be

application/vnd.openxmlformats-officedocument.wordprocessingml.document

and the file extension should be .docx

Solution 2

Maybe your uotput file get wrong header. I have something like this in my script, which i use to download files:

$tmp = explode(".",$file['filename']);
switch ($tmp[count($tmp)-1]) {
  case "pdf": $ctype="application/pdf"; break;
  case "exe": $ctype="application/octet-stream"; break;
  case "zip": $ctype="application/zip"; break;
  case "docx":
  case "doc": $ctype="application/msword"; break;
  case "csv":
  case "xls":
  case "xlsx": $ctype="application/vnd.ms-excel"; break;
  case "ppt": $ctype="application/vnd.ms-powerpoint"; break;
  case "gif": $ctype="image/gif"; break;
  case "png": $ctype="image/png"; break;
  case "jpeg":
  case "jpg": $ctype="image/jpg"; break;
  case "tif":
  case "tiff": $ctype="image/tiff"; break;
  case "psd": $ctype="image/psd"; break;
  case "bmp": $ctype="image/bmp"; break;
  case "ico": $ctype="image/vnd.microsoft.icon"; break;
  default: $ctype="application/force-download";
}

header("Pragma: public"); // required
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private",false); // required for certain browsers
header("Content-Type: $ctype");
header("Content-Disposition: attachment; filename=\"".$filename."\";" );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$fsize);
ob_clean();
flush();
readfile( 'files/'.$file['filename'] );

Filename I take from db.

Share:
16,415
Yoong Kim
Author by

Yoong Kim

Updated on June 04, 2022

Comments

  • Yoong Kim
    Yoong Kim almost 2 years

    This question is trivial and has many answers, all the same or nearly but for my case, it doesn't solve as expected? Goal: send WORD file as attachment with PHP (simple...) Mean: here is the code:

    // send the file to the browser
    header("Cache-Control: no-store");
    header("Content-Type: application/octet-stream");
    //header("Content-type: application/msword");
    header('Content-Disposition: attachment; filename="'. basename($filename) . '"');
    header('Content-Transfer-Encoding: binary');
    header('Content-Length: '. filesize($filename));
    ob_clean();
    flush();
    readfile($filename);
    exit();
    

    Well, everything seems right but the file I saved on my computer cannot be read by MS-WORD: it reads some special chars like:

    PK ! /Œt1« á [Content_Types].xml ...

    But if I open the original from the server, everything is ok. I missed something obvious... Any advice is welcome because I tried nearly ALL the methods I read...but still the same result.

    • Julian Reschke
      Julian Reschke almost 12 years
      Probably unrelated but still worth doing: remove undefined header fields like Content-Transfer-Encoding, and unneeded ones (such as Cache-Control).
  • Mark Baker
    Mark Baker almost 12 years
    mime type for .docx is NOT application/msword, it's application/vnd.openxmlformats-officedocument.wordprocessing‌​ml.document likewise you're wrong for xlsx as well
  • miszczu
    miszczu almost 12 years
    thx, where can i find documentation for that? I took it some time ago from php manual.
  • Mark Baker
    Mark Baker almost 12 years
    MIME types for all OfficeOpenXML filetypes technet.microsoft.com/en-us/library/ee309278.aspx or for all Office (BIFF and OfficeOpenXML) filetypes blogs.msdn.com/b/vsofficedeveloper/archive/2008/05/08/…
  • Yoong Kim
    Yoong Kim almost 12 years
    Sounds logical but same problem. Actually, the chars displayed are bit different now: "PK ‹ñ@ docProps/PK ! -‘Ë€ ã docProps/app.xmlRÁNã0 ½¯´ÿ åN€¶¢hj´ ­ö° H pD–3I, e» ù{&Ú Á". It seems that I can read more but always the same problem. Still, on the server, the file is ok.