MIME types

string mime_content_type ( string filename)

The Multipurpose Internet Mail Extensions (MIME) system was designed to allow the formatting of emails so that they can include files easily, and is made up of several parts. In order to be able to instruct email clients what types of files are attached, MIME types were created - short, textual descriptions of the files types that can be recognised by everyone. MIME types are so popular that they are used across the WWW as a whole now, and many operating systems rely on them to decide how to open a file. In emails, attachments are literally copied into the message as an encoded string, with MIME boundary markers being used to tell mail readers where each attachment starts and stops.

To help distinguish what a certain file is and how it should be handled, web servers and email clients both send and receive MIME types - basic text strings that identify what a given file is. For example, if you wanted to send a JPEG from a script, you would need to find out its MIME type ("image/jpeg"), and send that to the browser before the picture itself.

There are MIME types for all sorts of formats, from "application/zip" for zip files to "video/quicktime" for Quicktime .mov files, and "application/x-tar" for Tar files. It is the job of the Internet Assigned Numbers Authority (IANA) to assign official MIME types, and it also keeps a list of all the registered MIME types on its web site. At the time of writing, this list was available at http://www.iana.org/assignments/media-types - worth taking a look.

There are hundreds, possibly even thousands of MIME types out there, simply because there are so many file formats out there, but there are a certain few that stand out as being popular, which are:


Microsoft Excel data file


Wave sound file


TrueType Font


GIF image


JPEG image


TIFF image


Bitmap image


PNG image


HTML file


Plain text


Microsoft Word data file


MPEG video


Quicktime video

As you can see, the meanings are self-explanatory from the MIME type - it is human-readable and machine-readable, which is a big asset. MIME types are used in many places other than in emails - web servers, for example, make very heavy use of MIME types in order to know how to handle files as they are requested, and also so they know what kind of documents clients can and cannot receive.

Naturally it is undesirable to have to keep looking up long lists to find the MIME type you want every time you get a file, but PHP comes to the rescue with a special MIME look-up function, mime_content_type(). This is based upon the Apache module mod_mime_magic, which itself is based upon the Unix file command. If you have never used this before, the principle is that many types of files have a unique identifier in the first few bytes, referred to as a "magic number", that specifies what type of file it is. Bitmaps, for example, start with "BM", and MS DOS executables start with "MZ". By having a large lookup table of a selection of these magic numbers, it is quite easy to get an idea what kind of file is being examined, and thus what it is MIME type should be.

To get the MIME magic extension working, you must either configure PHP with the switch -with-mime-magic (Unix), or enable the extension in your php.ini file (Windows). On Windows, you will also need to edit one other entry in your php.ini file - "mime_magic.magicfile" should be set to the directory where PHP was installed, with the subdirectory "extras". So if you installed PHP into c:\php, this would need to be set to "c:/php/extras/magic.mime". On Unix, this extension relies the file "magic" shipped with Apache. If PHP fails to find this for some reason, try setting the php.ini entry also.

Once you have the MIME magic extension working, you just need to pass a filename to mime_content_type() to get its MIME type as the return value, like this:

    print mime_content_type("myfiles.zip");
    print mime_content_type("poppy.jpg");

Running that script, given that you have actually such files, should output application/zip and image/jpeg.


Want to learn PHP 7?

Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!

If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!

Next chapter: Easier mail sending with PEAR::Mail >>

Previous chapter: Sending mail

Jump to:


Home: Table of Contents

Copyright ©2015 Paul Hudson. Follow me: @twostraws.