Mime_TypesLocated in: Program_Root/Mime_Types.php
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)
default
0.1
Keyvan Minoukadeh <keyvan@k1m.com>
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 |
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') ));
|
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;
|
array get_extensions ( $type ) |
Get extensions - returns array containing extension(s)
Example: $exts = $mime->get_extensions('application/postscript'); echo implode(', ', $exts);
|
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');
|
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.
|
bool has_extension ( $ext ) |
Has extension - returns true if extension $ext exists, false otherwise
Example: if ($mime->has_extension('pdf')) echo 'Got it!';
|
bool has_type ( $type ) |
Has type - returns true if type $type exists, false otherwise
Example: if ($mime->has_type('image/gif')) echo 'Got it!';
|
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');
|
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'));
|
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
|
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.
|
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') ));
|