PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Tools and Utilities » MIME_Type » Manual

Utility class for dealing with MIME types. MIME_Type provides methods to detect the MIME (Multipurpose Internet Mail Extension) type of files, retrieving information about MIME types and parsing them.

Detecting MIME types

Detecting MIME types – How to determine the MIME type of a file

Detecting the MIME type of a file

The most simple way to detect the MIME type of any file is to use MIME_Type's static autoDetect() method. It will try to determine the file's type and return it as a string. If an error occurs, a PEAR_Error object is returned.

By default, only the plain MIME type will be returned, without any comments or parameters. If you pass true as second parameter to the method, all available MIME parameters will be appended to the returned type.

Detecting the MIME type of a file

<?php
require_once 'MIME/Type.php';

$filename = '/path/to/some/file.jpg';
echo MIME_Type::autoDetect($filename);
?>

The MIME type of the given file will be echoed.

Matching a MIME type

If you want to check if a certain MIME type matches a wildcard type, use the static wildcardMatch(). It takes the wildcard as first, and the type to be checked as second parameter. It returns true if the wildcard matches the MIME type, false if not.

Matching a wildcard type

<?php
require_once 'MIME/Type.php';

$filename = '/path/to/some/file.jpg';
$type     = MIME_Type::autoDetect($filename);

if (MIME_Type::wildcardMatch('image/*', $type)) {
    echo 'File ' . $filename . ' is an image.';
} else {
    echo 'File is no image.';
}
?>

Getting infos about a MIME type

Getting infos about a MIME type – How to get more information about a MIME type

What a MIME type can be

Using MIME_Type you can get different data about a MIME type. All methods mentioned here can be called statically.

Editing MIME types

Editing MIME types – How to change things

Editing a MIME type

With MIME_Type, you can edit existing MIME types or create new ones from scratch. Just create a new instance of MIME_Type - with the MIME type string as only parameter, if you have one - or no parameter if you want to begin a new one.

To set or retrieve the media type (part before the slash), use the object's $media property. The same applies to the subtype, the property name is $subType.

If you are done editing your MIME type, use get() to retrieve the MIME type's string representation.

Working with parameters

To retrieve parameters, use the $parameters property. It is an array consisting of MIME_Type_Parameter objects if there are any.

You can remove any parameter by calling removeParameter() and passing the parameter's name as only parameter.

Parameters are added with addParameter(). It takes the $name, $value and an optional $comment as parameters.

Again: If you are done editing the parameters, use get() to retrieve the MIME type's string representation.

There are three static methods that help you working with parameters: