从PHP中的文件名获取MIME类型 [英] Getting mime type from file name in php

查看:114
本文介绍了从PHP中的文件名获取MIME类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我具有以下功能,可从文件名生成MIME类型:

I have the following function to produce the mime type from a file name:

    function get_mime_type($file) {
      if (function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mimetype = finfo_file($finfo, $file);
        finfo_close($finfo);
      }
      else {
        $mimetype = mime_content_type($file);
      }
      if (empty($mimetype)) $mimetype = 'application/octet-stream';
      return $mimetype;
    }

我在代码的这一部分调用此函数:

I call this function at this portion of my code:

        $out['uploads'][] = array(
          'filename' => $fldrow['field_value'],
          'mimetype' => get_mime_type($fldrow['field_value']),
          'id'       => $fldrow['ID'],
        );

$fldrow['field_value']包含'card.pdf'

我期待'application/pdf'

我得到了'application/octet-stream'

我还使用模式= 1尝试了这种更精细的方法: PHP Mime类型检查的替代方法?

I also tried this more elaborate approach using Mode=1: PHP Mime type checking alternative way of doing it?

模式= 1时结果相同,模式= 0时结果为空白.

Same results in Mode=1 and blank in Mode=0.

在这里我可能做错了什么?

What may I be doing wrong here?

编辑 根据Dymen1的回应,并查看了该方向的其他帖子后,我的解决方案如下:

EDIT My solution based on Dymen1's response and after looking at other posts in that direction is the following:

function get_mime_type($filename) {
    $idx = explode( '.', $filename );
    $count_explode = count($idx);
    $idx = strtolower($idx[$count_explode-1]);

    $mimet = array( 
        'txt' => 'text/plain',
        'htm' => 'text/html',
        'html' => 'text/html',
        'php' => 'text/html',
        'css' => 'text/css',
        'js' => 'application/javascript',
        'json' => 'application/json',
        'xml' => 'application/xml',
        'swf' => 'application/x-shockwave-flash',
        'flv' => 'video/x-flv',

        // images
        'png' => 'image/png',
        'jpe' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpg' => 'image/jpeg',
        'gif' => 'image/gif',
        'bmp' => 'image/bmp',
        'ico' => 'image/vnd.microsoft.icon',
        'tiff' => 'image/tiff',
        'tif' => 'image/tiff',
        'svg' => 'image/svg+xml',
        'svgz' => 'image/svg+xml',

        // archives
        'zip' => 'application/zip',
        'rar' => 'application/x-rar-compressed',
        'exe' => 'application/x-msdownload',
        'msi' => 'application/x-msdownload',
        'cab' => 'application/vnd.ms-cab-compressed',

        // audio/video
        'mp3' => 'audio/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',

        // adobe
        'pdf' => 'application/pdf',
        'psd' => 'image/vnd.adobe.photoshop',
        'ai' => 'application/postscript',
        'eps' => 'application/postscript',
        'ps' => 'application/postscript',

        // ms office
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'docx' => 'application/msword',
        'xlsx' => 'application/vnd.ms-excel',
        'pptx' => 'application/vnd.ms-powerpoint',


        // open office
        'odt' => 'application/vnd.oasis.opendocument.text',
        'ods' => 'application/vnd.oasis.opendocument.spreadsheet',
    );

    if (isset( $mimet[$idx] )) {
     return $mimet[$idx];
    } else {
     return 'application/octet-stream';
    }
 }

推荐答案

如果查看文档,您会发现您没有做错任何事情. 但是,如果您进行更多研究:

If you check the documentation, you can see that you are not doing anything wrong. But if you do a bit more research:

https://stackoverflow.com/a/3664655/3784145

您可以看到您所获得的mime类型是正确的,但是扩展名不需要与mime类型匹配,如下所述:

you can see that the mime type you get is correct, but the extension doesn't need to match with the mime type as explained here:

http://nl3.php.net/manual/zh-CN/function.mime-content-type.php#85879

因此,我将使用文件后缀来确定文件的mime类型. (如第一个示例所示)

I would therefore use the files suffix to determine the files mime type. (as seen in the first example)

这篇关于从PHP中的文件名获取MIME类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆