user1304135
user1304135

Reputation: 15

Cant open .doc file after download in php

I have a script to download files with php, and it works great. But when I download a .doc type file and I open it in MS Word it says that I have to select a encoding and most of the text is in this format:

--- L ß{ ß{ Gü ² ÿÿ ÿÿ ÿÿ • Ê Ê ( ( Ð Ð Ð Ä ÿÿÿÿ ”¬ ”¬ ”¬ ”¬ T. èM ” ”- â |W ’W ’W ’W ’W ý÷ ý÷ ý÷ è ---

This are my headers:

header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$newFile\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
$mtype  = application/msword

Upvotes: 1

Views: 2520

Answers (1)

Michal
Michal

Reputation: 3368

You have to add header('Content-type: application/octet-stream');.

Not sure what your $mtype = application/msword is supposed to do...

And change header("Content-Disposition: attachment; filename=\"$newFile\""); to header('Content-Disposition: attachment; filename="'.$newFile'."');

After the headers just use:

ob_clean();
ob_end_flush();
readfile($file_path);
exit;

Upvotes: 3

Related Questions