【问题标题】:mime_content_type issue with some extension某些扩展的 mime_content_type 问题
【发布时间】:2012-02-05 07:07:45
【问题描述】:

我尝试 mime_content_type() / finfo_open()。 .doc 可以,但 .docx 返回 'application/zip' 而 .xls 则没有

有什么问题?是不是我的浏览器有问题?

【问题讨论】:

    标签: php mime-types file-extension


    【解决方案1】:

    这个问题基本一样:PHP 5.3.5 fileinfo() MIME Type for MS Office 2007 files - magic.mime updates?

    而且似乎没有解决办法。这不是你的浏览器,它是一个试图猜测的 mime“魔法”文件,没有办法区分 docx 和 zipfile 之间的区别,因为 docx 实际上是一个 zipfile!

    【讨论】:

      【解决方案2】:

      如果您像我一样,可能会或可能不会出于任何原因使用 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 的调用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-03-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多