tags:

views:

312

answers:

1

I'm looking for a code snippet which categorizes mime types.

For example,

application/msword

application/vnd.oasis.opendocument.text

application/pdf

Both of them are office files. When I pass these mime type to the function, I want it to return a result which is 'office', 'image', 'application', 'compressed' etc.

However,as you know, there are hundreds of mime types and I can not collect all of them.

Do you know where can I find it?

+1  A: 

I don't know of any existing script that would classify these types as you wish. You may need to create this function yourself based on the distinctions you require, e.g. application/msword -> office rather than application, etc.

As your classifications are fairly arbitrary and particular to your own use-case, you'll likely have to classify them yourself into your desired categories, e.g. by using a function something like the below:

/**
 * Classify mime types into pre-determined categories
 * 2-d array used for simplicity of example, error
 * checking omitted so unrecognised string returns
 * empty value here...
 */
function categorize_mime_types($mime)
{
    // Classify mime types into desired categories, key-val pairings
    $mimes = array("application/msword"=>"office",
                   "application/vnd.oasis.opendocument.text"=>"office",
                   ....
                   "image/jpeg"=>"image");
    return $mimes[$mime];
}

Collecting all of the mime types to do this could be quite time-consuming, although there are a number of website which have large lists to get you started.

ConroyP
Ah, sitepoint refence did not cateogorized them, too :( How can I do that, actually I don't know :(
Ahmet Alp Balkan