2012-01-08 138 views
0

我尝试mime_content_type()/ finfo_open()。对于.doc可以,但返回.docx的“应用程序/ zip”,对于.xls没有任何内容。mime_content_type问题与一些扩展

问题是什么?这是我的浏览器的问题吗?

回答

0

如果你和我一样,无论出于何种原因可能会或可能不会使用php> = 5.3.0服务器,并希望为所有服务器使用一组代码,并且可能坚持让mime_content_type函数以某种方式为服务器没有Fileinfo的话,那么你可以使用像我的这样的解决方案,这是一个替代函数,它在php> = 5.3.0上使用Fileinfo;在较低版本上,如果文件名以特定的字符串是你想要覆盖的东西,它返回你的硬编码值,并且为所有其他类型调用mime_content_type()。但是,如果文件的类型是mime_content_type()未正确检测并且文件名不在扩展名中结束,那么这当然不起作用,但这应该非常罕见。

这样的解决方案可能是这个样子:

function _mime_content_type($filename) 
{ 

     //mime_content_type replacement that uses Fileinfo native to php>=5.3.0 
    if(phpversion() >= '5.3.0') 
    { 

     $result = new finfo(); 

     if (is_resource($result) === true) 
     { 
      return $result->file($filename, FILEINFO_MIME_TYPE); 
     } 

    } 

    else 
    { 

     if(substr($filename, -5, 5) == '.docx') 
      return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document'; 
     else if(substr($filename, -5, 5) == '.xlsx') 
      return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'; 
     else if(substr($filename, -5, 5) == '.pptx') 
      return 'application/vnd.openxmlformats-officedocument.spreadsheetml.presentation'; 
     //amend this with manual overrides to your heart's desire 


     return mime_content_type($filename); 

    } 

} 

,然后你只需要更换所有来电通过调用_mime_content_type到mime_content_type。