【问题标题】:How do I determine the extension(s) associated with a MIME type in PHP?如何确定与 PHP 中的 MIME 类型关联的扩展名?
【发布时间】:2010-11-11 23:40:56
【问题描述】:

在我可以使用的 PHP 中,是否有快速而肮脏的 MIME 类型到扩展的映射?

【问题讨论】:

  • 人们只是想将 extensions 映射到 MIME 类型,而不是反过来,应该注意现在内置支持他们应该利用这一点 - 请参阅 Jorge's answer 而不是公认的。
  • @MarkAmery 但是如前所述, finfo_file() 要求文件存在。情况并非总是如此。 Chaos 的回答仍然更符合 OP,并且在 8 年后仍然有效。

标签: php mime mime-types filenames file-type


【解决方案1】:

不是内置的,但你自己的也不是很难:

function system_extension_mime_types() {
    # Returns the system MIME type mapping of extensions to MIME types, as defined in /etc/mime.types.
    $out = array();
    $file = fopen('/etc/mime.types', 'r');
    while(($line = fgets($file)) !== false) {
        $line = trim(preg_replace('/#.*/', '', $line));
        if(!$line)
            continue;
        $parts = preg_split('/\s+/', $line);
        if(count($parts) == 1)
            continue;
        $type = array_shift($parts);
        foreach($parts as $part)
            $out[$part] = $type;
    }
    fclose($file);
    return $out;
}

function system_extension_mime_type($file) {
    # Returns the system MIME type (as defined in /etc/mime.types) for the filename specified.
    #
    # $file - the filename to examine
    static $types;
    if(!isset($types))
        $types = system_extension_mime_types();
    $ext = pathinfo($file, PATHINFO_EXTENSION);
    if(!$ext)
        $ext = $file;
    $ext = strtolower($ext);
    return isset($types[$ext]) ? $types[$ext] : null;
}

function system_mime_type_extensions() {
    # Returns the system MIME type mapping of MIME types to extensions, as defined in /etc/mime.types (considering the first
    # extension listed to be canonical).
    $out = array();
    $file = fopen('/etc/mime.types', 'r');
    while(($line = fgets($file)) !== false) {
        $line = trim(preg_replace('/#.*/', '', $line));
        if(!$line)
            continue;
        $parts = preg_split('/\s+/', $line);
        if(count($parts) == 1)
            continue;
        $type = array_shift($parts);
        if(!isset($out[$type]))
            $out[$type] = array_shift($parts);
    }
    fclose($file);
    return $out;
}

function system_mime_type_extension($type) {
    # Returns the canonical file extension for the MIME type specified, as defined in /etc/mime.types (considering the first
    # extension listed to be canonical).
    #
    # $type - the MIME type
    static $exts;
    if(!isset($exts))
        $exts = system_mime_type_extensions();
    return isset($exts[$type]) ? $exts[$type] : null;
}

【讨论】:

  • 我认为这是一个旧答案。现在你应该使用fileinfo php.net/manual/en/ref.fileinfo.php
  • @JorgeB.G.这要求文件存在,不是吗?
  • @danronmoon。是的。
  • 我写了一个小包根据文件扩展名猜测MIME类型(如果文件不存在或无法访问):github.com/mzur/guess-mime
【解决方案2】:

您可以使用mime_content_type,但它已被弃用。请改用fileinfo

function getMimeType($filename) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $filename);
    finfo_close($finfo);
    return $mime;
}

【讨论】:

  • 值得注意的是,OP 实际上要求将 MIME 类型 映射到 文件扩展名。这仍然涵盖了最常见的用例(并且可能是 OP 面临的那个),所以它当然值得存在并且我已经 +1 了,但它不是严格地回答这个问题这被问为迂腐解释。
  • 注意:finfo_file()mime_content_type() 要求该文件存在。
  • 它在哪里说它已被弃用?
  • 这会失败,除非 $filename 是它可以读取的字符串。它不适用于与服务器上的文件不对应的通用字符串。
【解决方案3】:

你可能想使用这个库:https://github.com/ralouphie/mimey

示例用法:

$mimes = new \Mimey\MimeTypes;

// Convert extension to MIME type:
$mimes->getMimeType('json'); // application/json

// Convert MIME type to extension:
$mimes->getExtension('application/json'); // json

这是因为显然所提供的 PHP 函数的质量值得怀疑。

【讨论】:

    【解决方案4】:

    如果您正在处理各种有限的图像扩展并且需要一些非常简单的东西,我认为这就足够了。

       switch($info['mime'])
       {
        case 'image/gif'    : $extension = 'gif';   break;
        case 'image/png'    : $extension = 'png';   break;
        case 'image/jpeg'   : $extension = 'jpg';   break;
    
        default :
            throw new ApplicationException('The file uploaded was not a valid image file.');
        break;
        }
    

    【讨论】:

      【解决方案5】:

      使用这个文件: https://github.com/ralouphie/mimey/blob/develop/mime.types.php

      像这样:

      $mimes=include('mime.types.php');
      

      或复制内容:

      $mime= array (
        'mimes' => 
        array (
          'ez' => 
          array (
            0 => 'application/andrew-inset',
          ),
          'aw' => 
          array (
            0 => 'application/applixware',
          ),
          'atom' => 
          array (
            0 => 'application/atom+xml',
          ),
          'atomcat' => 
          array (
            0 => 'application/atomcat+xml',
          )
      
        ...
      

      以及从流中获取的示例:

       $finfo = new \finfo(FILEINFO_MIME_TYPE);
       $mime=$finfo->buffer($data);
       $mimes=include(__DIR__."/mime.types.php");
       echo ($mime); //mime
       echo ($mimes['extensions'][$mime]); // file extension
      

      【讨论】:

        猜你喜欢
        • 2011-02-04
        • 1970-01-01
        • 2011-05-14
        • 1970-01-01
        • 2013-11-11
        • 2012-11-18
        • 1970-01-01
        相关资源
        最近更新 更多