0

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.

  • a docx is literally just a zipfile that contains xml and other stuff. you can't determine what TYPE of office file it actually is without opening up the zip and rummaging around the xml. – Marc B Oct 17 '13 at 18:43
  • I'm a littlebit confused.. Are you saying that the downloaded file is broken? Can you open the editor first, and then the file? Are you certain the originally inserted file was not corrupt before you inserted it? – bjori Oct 21 '13 at 23:03

0 Answers0