1LWP::MediaTypes(3) User Contributed Perl Documentation LWP::MediaTypes(3)
2
3
4
6 LWP::MediaTypes - guess media type for a file or a URL
7
9 use LWP::MediaTypes qw(guess_media_type);
10 $type = guess_media_type("/tmp/foo.gif");
11
13 This module provides functions for handling media (also known as MIME)
14 types and encodings. The mapping from file extensions to media types
15 is defined by the media.types file. If the ~/.media.types file exists
16 it is used instead. For backwards compatibility we will also look for
17 ~/.mime.types.
18
19 The following functions are exported by default:
20
21 guess_media_type( $filename )
22 guess_media_type( $uri )
23 guess_media_type( $filename_or_uri, $header_to_modify )
24 This function tries to guess media type and encoding for a file or
25 a URI. It returns the content type, which is a string like
26 "text/html". In array context it also returns any content
27 encodings applied (in the order used to encode the file). You can
28 pass a URI object reference, instead of the file name.
29
30 If the type can not be deduced from looking at the file name, then
31 guess_media_type() will let the "-T" Perl operator take a look. If
32 this works (and "-T" returns a TRUE value) then we return
33 text/plain as the type, otherwise we return
34 application/octet-stream as the type.
35
36 The optional second argument should be a reference to a
37 HTTP::Headers object or any object that implements the $obj->header
38 method in a similar way. When it is present the values of the
39 'Content-Type' and 'Content-Encoding' will be set for this header.
40
41 media_suffix( $type, ... )
42 This function will return all suffixes that can be used to denote
43 the specified media type(s). Wildcard types can be used. In a
44 scalar context it will return the first suffix found. Examples:
45
46 @suffixes = media_suffix('image/*', 'audio/basic');
47 $suffix = media_suffix('text/html');
48
49 The following functions are only exported by explicit request:
50
51 add_type( $type, @exts )
52 Associate a list of file extensions with the given media type.
53 Example:
54
55 add_type("x-world/x-vrml" => qw(wrl vrml));
56
57 add_encoding( $type, @ext )
58 Associate a list of file extensions with an encoding type.
59 Example:
60
61 add_encoding("x-gzip" => "gz");
62
63 read_media_types( @files )
64 Parse media types files and add the type mappings found there.
65 Example:
66
67 read_media_types("conf/mime.types");
68
70 Copyright 1995-1999 Gisle Aas.
71
72 This library is free software; you can redistribute it and/or modify it
73 under the same terms as Perl itself.
74
75
76
77perl v5.12.4 2010-05-05 LWP::MediaTypes(3)