MIME type to file extensions

Datetime:2016-08-23 00:57:53          Topic: PHP  AWK           Share

Here :

Downoad mime.types filelocally here or

here (apache svn)

Download PHP arrary mapping mime-type to file extensions locallylocally here.

Download CSV mapping mime-type to file extensionslocally here

Note : in order to get mime type of a file using a shell command : file –mime-type b.png

#1 FILE EXTENSIONS

Starting from file mime.types (downloadlocally here) with following 1 shell line:

(echo "\$mime2ext = [" && grep -v "#" mime.types | awk '{print "\t \""$1"\"=>\""$2"\","}' && echo "\"\"=>\"undefine\"];") >mime2extension.inc

we can get mapping (PHP) between mime-type and file extensions :

“video/mp4″=>”mp4”,

“video/mpeg”=>”mpeg”,

“video/x-flv”=>”flv”,

“video/x-m4v”=>”m4v”,

“video/x-matroska”=>”mkv”,

The complete mapping in PHP can be downloadedhere.

If you preffer the mime-type and file extension mapping in CSV file:

grep -v "#"  mime.types | awk '{print $1","$2}' > mime2fileext.csv

Download CSVlocally here.

#2 MIME (in a nutshell)

MIME (Multipurpose Internet Mail Extensions) is an internet standard and an email extension in order to allow :

  • Text in character sets other than ASCII
  • Non-text attachments: audio, video, images, application programs etc.
  • Message bodies with multiple parts
  • Header information in non-ASCII character sets

MIME is designed for SMTP but is also used outside of SMTP such as HTTP.

#2.1 MIME headers

  • MIME-Version
  • Content-Type
  • Content-Transfer-Encoding
  • Content-ID
  • Content-Description
  • Content-Disposition

Example (header):

MIME-Version: 1.0

Content-Type: multipart/mixed; boundary=”stop-here”

#2.2 Body Parts

The MIME header in this simple example reports :

Content-Type: multipart/mixed; -> there are parts that are separate by a special string

boundary=” stop-here -> stop-here is the special string that delimit each part

Each body parts starts with followed by the boundary ( –stop-here ) and body part can contains itself the MIME header (Content-Type: text/plain;….). The last body part is defined by boundary followed by ( –stop-here– )

Example :

This is a multipart message in MIME format with 2 body parts.

–stop-here

Content-Type: text/plain

this is the body text

–stop-here

Content-Type: text/plain;

Content-Disposition: attachment; filename=”read.me”

This readme is an attachment

–stop-here–

Content-Type: text/plain ” is the mime type of data in the body.

#3 MIME TYPE

MIME type is a two-part (type and subtype) identifier for file formats. This provides information on wich type of native data is contained in de body (for example in order to select the right player or viewer).

Example :

Content-Type: video / mpeg

  • video is the type (video data. Video requires the capability to display moving images, typically including specialized hardware and software.)
  • mpeg is the subtype (type of compression)

#3.1 How to get the mime type of a file from the command line?

With the command file

Example :

Busybook:Desktop busycrack$ file –mime-type note.txt

note.txt: text/plain

Busybook:Desktop busycrack$ file –mime-type b.png

b.png: image/png





About List