2009-07-18 72 views
23

MIME類型是否存在快速和髒的映射到PHP中的擴展,我可以使用?如何確定與PHP中MIME類型關聯的擴展?

+0

PEO我們只想將*擴展*映射爲* MIME類型*,而不是相反的方式,應該注意的是,現在有內置的支持,他們應該利用這些支持 - 參見[Jorge's answer](http:// stackoverflow.com/a/20494035/1709587)而不是接受的。 – 2014-06-21 22:05:09

+1

@MarkAmery然而,如上所述,finfo_file()要求文件存在。情況並非總是如此。混沌的答案更多地指向OP,並且8年後仍然有效。 – Wranorn 2018-01-18 06:09:26

回答

21

沒有內置,但它不是太難推出自己:

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

我認爲這是一個古老的答案。現在你應該使用`fileinfo` http://www.php.net/manual/en/ref.fileinfo.php – 2013-12-10 12:05:47

+6

@ JorgeB.G。這要求文件存在,不是嗎? – danronmoon 2014-10-17 16:44:45

6

您可以使用mime_content_type,但不建議使用。使用fileinfo代替:

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

如果您正在使用改變圖像的限制擴展和需要的東西很簡單的工作,我認爲這是不夠的。

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

你可能想使用這個庫: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函數顯然質量可疑。

0

使用本文件: 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