Class Mime_Types

Description

MIME Types class

This class allows you to:

  • Retrieve the appropriate MIME type of a file (based on it's extension, or by utilising the file command to guess it based on the file contents).
  • Retrieve extension(s) associated with a MIME type.
  • Load MIME types and extensions from a mime.types file.
Example:

1 $mime =& new Mime_Types('/usr/local/apache/conf/mime.types');
2 echo $mime->get_type('pdf'); // application/pdf
3 echo $mime->get_extension('text/vnd.wap.wmlscript');

See test_Mime_Types.php file for more examples.

TODO:

  • method to write MIME types to file.
  • get_file_type(): possibly preserving the parameters returned by the file command (e.g. text/plain; charset=us-ascii)

Located in Program_Root/Mime_Types.php (line 48)


	
			
Variable Summary
string $file_cmd
Method Summary
void Mime_Types ([mixed $mime_types = null])
string get_extension (string $type)
array get_extensions (string $type)
string get_file_type (string $file, [bool $use_ext = true])
string get_type (string $ext)
bool has_extension (string $ext)
bool has_type (string $type)
bool load_file (string $file)
void remove_extension (mixed $exts)
void remove_type ([string $type = null])
void scan (mixed $callback, mixed &$param)
void set (mixed $type, [mixed $exts = null])
Variables
string $file_cmd = '' (line 87)

Path to file command - empty string disables the use of the file command

array $file_options = array('b'=>null, 'i'=>null) (line 100)

File options, used with the file command

Example:

   ['i'] => null // this option asks file to produce a MIME type if it can
   ['b'] => null // this option tells file to be brief
 
will result in 'file -i -b test_file'

Methods
Constructor Mime_Types (line 122)

Constructor

Optional parameter can be either a string containing the path to the mime.types files, or an associative array holding the extension as key and the MIME type as value.

Example:


1 $mime =& new Mime_Types('/usr/local/apache/conf/mime.types');
or

1 $mime =& new Mime_Types(array(
2 'application/pdf' => 'pdf',
3 'application/postscript' => array('ai','eps')
4 ));

void Mime_Types ([mixed $mime_types = null])
  • mixed $mime_types
get_extension (line 352)

Get extension

Returns string containing a extension associated with $type

Example:


1 $ext = $mime->get_extension('application/postscript');
2 if ($ext) echo $ext;

  • return:

    false if $type not found

string get_extension (string $type)
  • string $type
get_extensions (line 372)

Get extensions - returns array containing extension(s)

Example:


1 $exts = $mime->get_extensions('application/postscript');
2 echo implode(', ', $exts);

array get_extensions (string $type)
  • string $type
get_file_type (line 190)

Get file type

Returns MIME type by trying to guess it using the file command. Optional second parameter should be a boolean. If true (default), get_file_type() will try to guess the MIME type based on the file extension if the file command fails to find a match.

Example:


1 echo $mime->get_file_type('/path/to/my_file', false);
or

1 echo $mime->get_file_type('/path/to/my_file.gif');

  • return:

    false if unable to find suitable match

string get_file_type (string $file, [bool $use_ext = true])
  • string $file
  • bool $use_ext:

    default: true

get_type (line 240)

Get type - returns MIME type based on the file extension

Example:


1 echo $mime->get_type('txt');
or

1 echo $mime->get_type('test_file.txt');
both examples above will return the same result.

  • return:

    false if extension not found

string get_type (string $ext)
  • string $ext
has_extension (line 317)

Has extension

Returns true if extension $ext exists, false otherwise

Example:


1 if ($mime->has_extension('pdf')) echo 'Got it!';

bool has_extension (string $ext)
  • string $ext
has_type (line 334)

Has type

Returns true if type $type exists, false otherwise

Example:


1 if ($mime->has_type('image/gif')) echo 'Got it!';

bool has_type (string $type)
  • string $type
load_file (line 453)

Load file containing mime types.

Example:


1 $result = $mime->load_file('/usr/local/apache/conf/mime.types');
2 echo (($result) ? 'Success!' : 'Failed');

bool load_file (string $file)
  • string $file
remove_extension (line 396)

Remove extension

Example:


1 $mime->remove_extension('txt');
or

1 $mime->remove_extension('txt exe html');
or

1 $mime->remove_extension(array('txt', 'exe', 'html'));

void remove_extension (mixed $exts)
  • mixed $exts:

    string holding extension(s) seperated by space, or array

remove_type (line 425)

Remove type

Example:


1 $mime->remove_type('text/plain');
or

1 $mime->remove_type('image/*');
2
or

1 $mime->remove_type();
2

void remove_type ([string $type = null])
  • string $type:

    if omitted, all types will be removed

scan (line 154)

Scan

Goes through all MIME types passing the extension and type to the callback function. The types will be sent in alphabetical order. If a type has multiple extensions, each extension will be passed seperately (not as an array).

The callback function can be a method from another object (eg. array(&$my_obj, 'my_method')). The callback function should accept 3 arguments:

  1. A reference to the Mime_Types object (&$mime)
  2. An array holding extension and type, array keys: [0]=>ext, [1]=>type
  3. An optional parameter which can be used for whatever your function wants :), even though you might not have a use for this parameter, you need to define your function to accept it. (Note: you can have this parameter be passed by reference)
The callback function should return a boolean, a value of 'true' will tell scan() you want it to continue with the rest of the types, 'false' will tell scan() to stop calling your callback function.

void scan (mixed $callback, mixed &$param)
  • mixed $callback:

    function name, or array holding an object and the method to call.

  • mixed $param:

    passed as the 3rd argument to $callback

set (line 279)

Set

Set extension and MIME type

Example:


1 $mime->set('text/plain', 'txt');
or

1 $mime->set('text/html', array('html','htm'));
or

1 $mime->set('text/html', 'html htm');
or

1 $mime->set(array(
2 'application/pdf' => 'oda',
3 'application/postscript' => array('ai','eps')
4 ));

void set (mixed $type, [mixed $exts = null])
  • mixed $type:

    either array containing type and extensions, or the type as string

  • mixed $exts:

    either array holding extensions, or string holding extensions seperated by space.

Documention generated on Wed, 16 Jul 2003 01:03:39 +0100 by phpDocumentor 1.2.0