确定Excel文件的MIME类型。

5
我可以帮忙翻译。这段内容是关于编程的,需要判断上传文件的类型。当上传.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文件的MIME类型,而是.xls文件的MIME类型。
那么,为什么mime_content_type()函数会返回.xlsx文件的application/vnd.ms-excel?真相在哪里?
4个回答

12
使用FileInfo代替mime_content_type(已被弃用)。
关于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 Web服务器上)可在/etc/mime.types中找到。

5

3

这里有一个包装器,可以正确识别 Microsoft Office 2007 文档。使用、编辑和添加更多文件扩展名/ MIME 类型都非常简单明了。

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);
}

2
正如您在mime_content_type函数页面上看到的警告,它现在已经过时,并被finfo函数取代。
$finfo = new finfo(); 
$fileinfo = $finfo->file($file, FILEINFO_MIME);

安装 finfo 扩展。
pecl install fileinfo

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接