كيف يمكنني تحديد الملحق (الملحق) المرتبط بنوع MIME في PHP؟

StackOverflow https://stackoverflow.com/questions/1147931

سؤال

هل هناك رسم خرائط سريعة وقذرة لأنواع MIME إلى الملحقات في PHP التي يمكنني الاستفادة منها؟

هل كانت مفيدة؟

المحلول

ليس مدمجا، لكنه ليس من الصعب للغاية لفك:

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

نصائح أخرى

يمكنك استخدام mime_content_type لكنه مهمل. يستخدم fileinfo في حين أن:

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

قد ترغب في استخدام هذه المكتبة: https://github.com/raleouphie/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 المقدمة مشكوك فيها.

إذا كنت ترغب في استخدام Webservice، فقد أنشأت هذا كجزء من خدمة Mimetype <-> Icon

http://stdicon.com/

علي سبيل المثال :

http://stdicon.com/ext/html.

إنه يعمل على Appengine، لذلك يجب أن يكون لها توافر عالي.

إذا كنت تعمل مع تختلف ملحقات محدودة للصور وتحتاج إلى شيء بسيط للغاية، أعتقد أن هذا يكفي.

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

استخدم هذا الملف:https://github.com/raleouphie/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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top