I am having trouble getting .docx files to be served properly from MongoDB. I have this as the code:
<?php
$m = new Mongo(//params);
$id = $_GET["FID"];
$patternDOCX = '/.docx$/i';
$patternDOC = '/.doc$/i';
$patternPDF = '/.pdf$/i';
$MIME = "";
$gridFS = $m->db->getGridFS();
$filePointer = $gridFS->findOne(array('_id' => new MongoId($id)));
$nameOriginal = $filePointer->file['filename'];
if(preg_match($patternDOCX, $nameOriginal)){
$MIME = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
} elseif(preg_match($patternDOC, $nameOriginal)){
$MIME = "application/msword";
} elseif (preg_match($patternPDF, $nameOriginal)){
$MIME = "application/pdf";
}
header('Content-type: '.$MIME);
header('Content-Disposition: attachment; filename='.$nameOriginal);
echo $gridFS->findOne(array('_id' => new MongoId($id)))->getBytes();
?>
When the requested file is a pdf, it downloads fine and is readable. However, when the requested file is .docx it downloads as a docx file but when it opens it cannot determine then encoding and none of the options it offers make the document come out right. Am I missing some kind of encoding header? Any help is appreciated.