Mime_TypesMIME Types class
This class allows you to:
See test_Mime_Types.php file for more examples.
TODO:
Located in /metajour/core/util/class.mimetypes.php (line 45)
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(
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"
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') ));
Get extension - returns string containing a extension associated with $type
Example: $ext = $mime->get_extension('application/postscript'); if ($ext) echo $ext;
Get extensions - returns array containing extension(s)
Example: $exts = $mime->get_extensions('application/postscript'); echo implode(', ', $exts);
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');
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.
Has extension - returns true if extension $ext exists, false otherwise
Example: if ($mime->has_extension('pdf')) echo 'Got it!';
Has type - returns true if type $type exists, false otherwise
Example: if ($mime->has_type('image/gif')) echo 'Got it!';
Load file - load file containing mime types.
Example: $result = $mime->load_file('/usr/local/apache/conf/mime.types'); echo (($result) ? 'Success!' : 'Failed');
Remove extension
Example: $mime->remove_extension('txt'); or $mime->remove_extension('txt exe html'); or $mime->remove_extension(array('txt', 'exe', 'html'));
Remove type
Example: $mime->remove_type('text/plain'); or $mime->remove_type('image/*'); // removes all image types or $mime->remove_type(); // clears all types
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.
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') ));
Remove type callback
Documentation generated on Thu, 9 Jun 2005 06:51:42 +0200 by phpDocumentor 1.2.3