在PHP中使用MongoDB提供docx文件

时间:2013-10-17 18:39:04

标签: php mongodb docx gridfs

我无法从MongoDB正确地提供.docx文件。我有这个代码:

<?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();
?> 

当请求的文件是pdf时,它下载得很好并且可读。但是,当请求的文件是.docx时,它会下载为docx文件,但是当它打开时,它无法确定编码,它提供的选项都不会使文档正确。我错过了某种编码头吗?任何帮助表示赞赏。

0 个答案:

没有答案