2012-01-08 141 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。