php 确定excel文件mime类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16251955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 10:46:48  来源:igfitidea点击:

Determine excel file mime type

phpexcelmime-types

提问by Oto Shavadze

I need determine what is type of uploaded file

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

When upload .xlsxfile, this code:

上传.xlsx文件时,此代码:

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

returns:

返回:

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

As I know (from here PHP xls, xlsx, ppt, pptx headers), application/vnd.ms-excelis not .xlsx, but .xlsfile mime type.

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

So, why returns mime_content_type()function application/vnd.ms-excelfor .xlsxfile? where the truth?

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

采纳答案by Mark Baker

mime_content_type()isn't particularly accurate, and has been deprecated in favour of Fileinfo()'s mime_content_type; although personally, I open the file and test explicitly for certain data elements in the files that might not be included as part of the mime_magic signature details

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

回答by tucuxi

Use FileInfoinstead of mime_content_type (which is deprecated).

使用FileInfo而不是 mime_content_type(已弃用)。

Regarding mime-types and extensions,

关于 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

(available at /etc/mime.types in your linux webserver)

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

回答by EpokK

Here is an wrapper that will properly identify Microsoft Office 2007 documents. It's trivial and straightforward to use, edit, and to add more file extentions/mimetypes.

这是一个可以正确识别 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);
}

回答by Rikesh

As you can see warning on mime_content_typefunction page it is outdatednow & it got replaced by finfofunction.

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

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

To install finfoextension.

安装finfo扩展。

pecl install fileinfo