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: $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)

Located in /metajour/core/util/class.mimetypes.php (line 45)


	
			
Variable Summary
 string $file_cmd
 array $mime_types
Method Summary
 Mime_Types 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])
 bool _remove_type_callback (object &$mime, array $ext_type, array $type_info)
Variables
string $file_cmd = '' (line 69)

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

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

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'

array $mime_types = array(
'txt' => 'text/plain',
'gif' => 'image/gif',
'jpg' => 'image/jpeg',
'html' => 'text/html',
'htm' => 'text/html')
(line 58)

MIME Types Initially we start with the more popular ones.

["txt"] => "text/plain", ["gif"] => "image/gif", ["jpg"] => "image/jpeg", ["html"] => "text/html", ["htm"] => "text/html"

  • access: private
Methods
Constructor Mime_Types (line 96)

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') ));

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

Get extension - returns string containing a extension associated with $type

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

  • return: false if $type not found
string get_extension (string $type)
  • string $type
get_extensions (line 302)

Get extensions - returns array containing extension(s)

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

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

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');

  • 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 199)

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.

  • return: false if extension not found
string get_type (string $ext)
  • string $ext
has_extension (line 260)

Has extension - returns true if extension $ext exists, false otherwise

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

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

Has type - returns true if type $type exists, false otherwise

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

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

Load file - load file containing mime types.

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

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

Remove extension

Example: $mime->remove_extension('txt'); or $mime->remove_extension('txt exe html'); or $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 341)

Remove type

Example: $mime->remove_type('text/plain'); or $mime->remove_type('image/*'); // removes all image types or $mime->remove_type(); // clears all types

void remove_type ([string $type = null])
  • string $type: if omitted, all types will be removed
scan (line 125)

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 227)

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') ));

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.
_remove_type_callback (line 396)

Remove type callback

  • access: private
bool _remove_type_callback (object &$mime, array $ext_type, array $type_info)
  • object $mime
  • array $ext_type
  • array $type_info

Documentation generated on Thu, 9 Jun 2005 06:51:42 +0200 by phpDocumentor 1.2.3