【问题标题】:Determine excel file mime type确定excel文件mime类型
【发布时间】:2013-04-21 13:02:58
【问题描述】:

我需要确定上传文件的类型

上传.xlsx文件时,这段代码:

 echo $_FILES['uploaded_file']['type']."<br>";  
 echo mime_content_type($_FILES['uploaded_file']['tmp_name']);

返回:

application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
application/vnd.ms-excel

据我所知(从这里PHP xls, xlsx, ppt, pptx headers),application/vnd.ms-excel 不是.xlsx,而是.xls 文件mime 类型。

那么,为什么要为.xlsx 文件返回mime_content_type() 函数application/vnd.ms-excel?真相在哪里?

【问题讨论】:

    标签: php excel mime-types


    【解决方案1】:

    使用FileInfo 代替 mime_content_type(即deprecated)。

    关于 mime 类型和扩展,

    application/vnd.ms-excel                                          xls xlb xlt
    application/vnd.ms-excel.addin.macroEnabled.12                    xlam
    application/vnd.ms-excel.sheet.binary.macroEnabled.12             xlsb
    application/vnd.ms-excel.sheet.macroEnabled.12                    xlsm
    application/vnd.ms-excel.template.macroEnabled.12                 xltm
    application/vnd.openxmlformats-officedocument.spreadsheetml.sheet xlsx
    

    (在您的 linux 网络服务器中的 /etc/mime.types 中可用)

    【讨论】:

      【解决方案2】:

      mime_content_type() 不是特别准确,已被Fileinfo()'s mime_content_type 弃用;尽管就个人而言,我打开文件并明确测试文件中可能未包含在 mime_magic 签名详细信息中的某些数据元素

      【讨论】:

        【解决方案3】:

        这是一个可以正确识别 Microsoft Office 2007 文档的包装器。使用、编辑和添加更多文件扩展名/mimetypes 简单直接。

        function get_mimetype($filepath) {
            if(!preg_match('/\.[^\/\\\\]+$/',$filepath)) {
                return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath);
            }
            switch(strtolower(preg_replace('/^.*\./','',$filepath))) {
                // START MS Office 2007 Docs
                case 'docx':
                    return 'application/vnd.openxmlformats-officedocument.wordprocessingml.document';
                case 'docm':
                    return 'application/vnd.ms-word.document.macroEnabled.12';
                case 'dotx':
                    return 'application/vnd.openxmlformats-officedocument.wordprocessingml.template';
                case 'dotm':
                    return 'application/vnd.ms-word.template.macroEnabled.12';
                case 'xlsx':
                    return 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet';
                case 'xlsm':
                    return 'application/vnd.ms-excel.sheet.macroEnabled.12';
                case 'xltx':
                    return 'application/vnd.openxmlformats-officedocument.spreadsheetml.template';
                case 'xltm':
                    return 'application/vnd.ms-excel.template.macroEnabled.12';
                case 'xlsb':
                    return 'application/vnd.ms-excel.sheet.binary.macroEnabled.12';
                case 'xlam':
                    return 'application/vnd.ms-excel.addin.macroEnabled.12';
                case 'pptx':
                    return 'application/vnd.openxmlformats-officedocument.presentationml.presentation';
                case 'pptm':
                    return 'application/vnd.ms-powerpoint.presentation.macroEnabled.12';
                case 'ppsx':
                    return 'application/vnd.openxmlformats-officedocument.presentationml.slideshow';
                case 'ppsm':
                    return 'application/vnd.ms-powerpoint.slideshow.macroEnabled.12';
                case 'potx':
                    return 'application/vnd.openxmlformats-officedocument.presentationml.template';
                case 'potm':
                    return 'application/vnd.ms-powerpoint.template.macroEnabled.12';
                case 'ppam':
                    return 'application/vnd.ms-powerpoint.addin.macroEnabled.12';
                case 'sldx':
                    return 'application/vnd.openxmlformats-officedocument.presentationml.slide';
                case 'sldm':
                    return 'application/vnd.ms-powerpoint.slide.macroEnabled.12';
                case 'one':
                    return 'application/msonenote';
                case 'onetoc2':
                    return 'application/msonenote';
                case 'onetmp':
                    return 'application/msonenote';
                case 'onepkg':
                    return 'application/msonenote';
                case 'thmx':
                    return 'application/vnd.ms-officetheme';
                    //END MS Office 2007 Docs
        
            }
            return finfo_file(finfo_open(FILEINFO_MIME_TYPE), $filepath);
        }
        

        【讨论】:

          【解决方案4】:

          正如您在mime_content_type 功能页面上看到的警告,它现在已过时,并已被finfo 功能取代。

          $finfo = new finfo(); 
          $fileinfo = $finfo->file($file, FILEINFO_MIME);
          

          安装finfo 扩展。

          pecl install fileinfo
          

          【讨论】:

            猜你喜欢
            • 2016-05-23
            • 2012-07-26
            • 2012-02-02
            • 2012-01-25
            • 2017-10-26
            • 2013-07-15
            • 2011-08-25
            • 2012-03-16
            • 1970-01-01
            相关资源
            最近更新 更多