The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

guess_media_type - guess media type for a file or a URL.

media_suffix - returns file extentions for a media type

SYNOPSIS

 use LWP::MediaTypes qw(guess_media_type);
 $type = guess_media_type("/tmp/foo.gif");

DESCRIPTION

This module provides functions for handling of media (also known as MIME) types and encodings. The mapping from file extentions to media types is defined by the media.types file. If the ~/.media.types file exist it is used as a replacement.

For backwards compatability we will also look for ~/.mime.types.

FUNCTIONS

guess_media_type($filename_or_url, [$header_to_modify])

This function tries to guess media type and encoding for given file. In scalar context it returns only the content-type. In array context it returns an array consisting of content-type followed by any content-encodings applied.

The guess_media_type function also accepts a URI::URL object as argument.

If the type can not be deduced from looking at the file name only, then guess_media_type() will take a look at the actual file using the -T perl operator in order to determine if this is a text file (text/plain). If this does not work it will return application/octet-stream as the type.

The optional second argument should be a reference to a HTTP::Headers object (or some HTTP::Message object). When present this function will set the value of the 'Content-Type' and 'Content-Encoding' for this header.

media_suffix($type,...)

This function will return all suffixes that can be used to denote the specified media type(s). Wildcard types can be used. In scalar context it will return the first suffix found.

Examples:

  @suffixes = media_suffix('image/*', 'audio/basic');
  $suffix = media_suffix('text/html');