如何确定在PHP中与MIME类型相关联的扩展(s) ?

时间:2023-01-22 07:21:43

Is there a quick and dirty mapping of MIME types to extensions in PHP that I can make use of?

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

6 个解决方案

#1


22  

Not built-in, but it's not terribly hard to roll your own:

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

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

#2


6  

You could use mime_content_type but it is deprecated. Use fileinfo instead:

您可以使用mime_content_type,但不赞成使用它。用fileinfo代替:

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

#3


2  

If you want to use a webservice, I just created this as part of my mimetype <-> icon service

如果您想使用webservice,我只是将它作为mimetype <->图标服务的一部分创建的

http://stdicon.com/

http://stdicon.com/

For example :

例如:

http://stdicon.com/ext/html

http://stdicon.com/ext/html

It runs on appengine so it should have high availability.

它在appengine上运行,因此应该具有高可用性。

#4


2  

If you are working with vary limited extensions of images and need something very simple, I think this is enough.

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

   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


2  

You might want to use this library: https://github.com/ralouphie/mimey

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

Example usage:

使用示例:

$mimes = new \Mimey\MimeTypes;

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

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

This because apparently the quality of the provided PHP functions is dubious.

这显然是因为提供的PHP函数的质量是可疑的。

#6


0  

use this file : https://github.com/ralouphie/mimey/blob/develop/mime.types.php

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

like this :

是这样的:

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

or copy content:

或复制内容:

$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',
    )

  ...

and an example of getting from a stream:

还有一个从流中获取的例子:

 $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

#1


22  

Not built-in, but it's not terribly hard to roll your own:

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

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

#2


6  

You could use mime_content_type but it is deprecated. Use fileinfo instead:

您可以使用mime_content_type,但不赞成使用它。用fileinfo代替:

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

#3


2  

If you want to use a webservice, I just created this as part of my mimetype <-> icon service

如果您想使用webservice,我只是将它作为mimetype <->图标服务的一部分创建的

http://stdicon.com/

http://stdicon.com/

For example :

例如:

http://stdicon.com/ext/html

http://stdicon.com/ext/html

It runs on appengine so it should have high availability.

它在appengine上运行,因此应该具有高可用性。

#4


2  

If you are working with vary limited extensions of images and need something very simple, I think this is enough.

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

   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


2  

You might want to use this library: https://github.com/ralouphie/mimey

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

Example usage:

使用示例:

$mimes = new \Mimey\MimeTypes;

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

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

This because apparently the quality of the provided PHP functions is dubious.

这显然是因为提供的PHP函数的质量是可疑的。

#6


0  

use this file : https://github.com/ralouphie/mimey/blob/develop/mime.types.php

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

like this :

是这样的:

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

or copy content:

或复制内容:

$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',
    )

  ...

and an example of getting from a stream:

还有一个从流中获取的例子:

 $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