Php Mongodb GridFS在下载.ppt和.doc文件时遇到问题?
我的控制器中有此功能,可以下载存储在mongodb网格中的文件:Php Mongodb GridFS在下载.ppt和.doc文件时遇到问题?,php,mongodb,powerpoint,mime-types,gridfs,Php,Mongodb,Powerpoint,Mime Types,Gridfs,我的控制器中有此功能,可以下载存储在mongodb网格中的文件: function download_presentation($ext,$store_filename) { $grid = $this->mongo->db->getGridFS(); //query the file object $objects = $grid->find(); //set co
function download_presentation($ext,$store_filename)
{
$grid = $this->mongo->db->getGridFS();
//query the file object
$objects = $grid->find();
//set content-type header, output in browser
switch ($ext) {
case 'pdf':
$mimeType = 'Content-type: application/pdf';
break;
case 'jpg':
$mimeType = 'Content-type: image/jpg';
break;
case 'png':
$mimeType = 'Content-type: image/png';
break;
case 'doc':
$mimeType = 'Content-type: application/msword';
break;
case 'docx':
$mimeType = 'Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document';
break;
case 'xls':
$mimeType = 'Content-type: application/vnd.ms-excel';
break;
case 'xlsx':
$mimeType = 'Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
break;
case 'ppt':
$mimeType = 'Content-type: application/x-mspowerpoint';
break;
case 'pptx': $mimeType = 'Content-type: application/vnd.openxmlformats- officedocument.presentationml.presentation';
break;
default:
$mimeType='Content-type: application/pdf';
}
while($object = $objects->getNext()) :
if(($object->file['filename'])==$store_filename){
$content=$object->getBytes();
header("Content-Length: " . strlen($content));
header($mimeType);
echo ($content);}
endwhile;
}
每当我下载.ppt文件时,都会说PowerPoint无法打开此文件,因为它不是.ppt。对于下载后打开的pptx文件,当我单击PowerPoint中的“修复”弹出窗口时,pptx文件会工作。在.doc/MS-Word中也会发生同样的情况。只有pdf文件才能正常运行。
有人能告诉我问题出在哪里吗?好吧,经过一年的阅读、理解和实现PHP,我能够回答我自己的问题,我已经用这个方法修复了错误,效果很好,希望这对以后的人有所帮助
$content= $object->getBytes();
header("Content-Length: " . strlen($content));
header($mimeType);
ob_clean();
echo($content);
尝试调用$object->而不是使用strlen函数。您能告诉我为什么需要使用
ob_clean
清空缓冲区吗?如果我们使用ob_clean();冲洗()代码>ob_clean清除缓冲区,在渲染图像之前,它也会清除多余的空间..您的解决方案在同样的情况下对我有帮助。通常情况下,这不应该发生,因为这意味着在脚本的某个地方进行了早期(当然是无意的)输出。您的解决方案帮助我发现,在我的例子中,不可见的输出是由一个带有UTF-8字符的UTF-8文件引起的。