Class Mime_Types

Mime_Types
Located in: Program_Root/Mime_Types.php
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:
  $mime =& new Mime_Types('/usr/local/apache/conf/mime.types');
  echo $mime->get_type('pdf');    // application/pdf
  echo $mime->get_extension('text/vnd.wap.wmlscript');      // wmls

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)

Method Summary

 void constructor Mime_Types ( [$mime_types = null] )
          Constructor
 string get_extension ( $type )
          Get extension - returns string containing a extension associated with $type
 array get_extensions ( $type )
          Get extensions - returns array containing extension(s)
 string get_file_type ( $file, [$use_ext = true] )
          Get file type - returns MIME type by trying to guess it using the file command.
 string get_type ( $ext )
          Get type - returns MIME type based on the file extension.
 bool has_extension ( $ext )
          Has extension - returns true if extension $ext exists, false otherwise
 bool has_type ( $type )
          Has type - returns true if type $type exists, false otherwise
 bool load_file ( $file )
          Load file - load file containing mime types.
 void remove_extension ( $exts )
          Remove extension
 void remove_type ( [$type = null] )
          Remove type
 void scan ( $callback, &$param )
          Scan - goes through all MIME types passing the extension and type to the callback function.
 void set ( $type, [$exts = null] )
          Set - set extension and MIME type

Inherited Method Summary


Method Detail

constructor Mime_Types

void  constructor Mime_Types ( [$mime_types = null] )

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:
  $mime =& new Mime_Types('/usr/local/apache/conf/mime.types');
 or
  $mime =& new Mime_Types(array(
       'application/pdf'        => 'pdf', 
       'application/postscript' => array('ai','eps')
       ));
Function Parameters:
- mixed $mime_types:
Function Info:

get_extension

string  get_extension ( $type )

Get extension - returns string containing a extension associated with $type
Example:
  $ext = $mime->get_extension('application/postscript');
  if ($ext) echo $ext;
Function Parameters:
- string $type:
Function Info:
Return - false if $type not found

get_extensions

array  get_extensions ( $type )

Get extensions - returns array containing extension(s)
Example:
  $exts = $mime->get_extensions('application/postscript');
  echo implode(', ', $exts);
Function Parameters:
- string $type:
Function Info:

get_file_type

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

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:
  echo $mime->get_file_type('/path/to/my_file', false);
 or
  echo $mime->get_file_type('/path/to/my_file.gif');
Function Parameters:
- string $file:
- bool $use_ext: default: true
Function Info:
Return - false if unable to find suitable match

get_type

string  get_type ( $ext )

Get type - returns MIME type based on the file extension.
Example:
  echo $mime->get_type('txt');
 or
  echo $mime->get_type('test_file.txt');
both examples above will return the same result.
Function Parameters:
- string $ext:
Function Info:
Return - false if extension not found

has_extension

bool  has_extension ( $ext )

Has extension - returns true if extension $ext exists, false otherwise
Example:
  if ($mime->has_extension('pdf')) echo 'Got it!';
Function Parameters:
- string $ext:
Function Info:

has_type

bool  has_type ( $type )

Has type - returns true if type $type exists, false otherwise
Example:
  if ($mime->has_type('image/gif')) echo 'Got it!';
Function Parameters:
- string $type:
Function Info:

load_file

bool  load_file ( $file )

Load file - load file containing mime types.
Example:
  $result = $mime->load_file('/usr/local/apache/conf/mime.types');
  echo (($result) ? 'Success!' : 'Failed');
Function Parameters:
- string $file:
Function Info:

remove_extension

void  remove_extension ( $exts )

Remove extension
Example:
  $mime->remove_extension('txt');
 or
  $mime->remove_extension('txt exe html');
 or
  $mime->remove_extension(array('txt', 'exe', 'html'));
Function Parameters:
- mixed $exts: string holding extension(s) seperated by space, or array
Function Info:

remove_type

void  remove_type ( [$type = null] )

Remove type
Example:
  $mime->remove_type('text/plain');
 or
  $mime->remove_type('image/*');
  // removes all image types
 or
  $mime->remove_type();
  // clears all types
Function Parameters:
- string $type: if omitted, all types will be removed
Function Info:

scan

void  scan ( $callback, &$param )

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.
Function Parameters:
- mixed $callback: function name, or array holding an object and the method to call.
- mixed $param: passed as the 3rd argument to $callback
Function Info:

set

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

Set - set extension and MIME type
Example:
  $mime->set('text/plain', 'txt');
 or
  $mime->set('text/html', array('html','htm'));
 or
  $mime->set('text/html', 'html htm');
 or
  $mime->set(array(
       'application/pdf'        => 'oda', 
       'application/postscript' => array('ai','eps')
       ));
Function Parameters:
- 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.
Function Info: