MIME Types class
This class allows you to:
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:
Located in Program_Root/Mime_Types.php (line 48)
Path to file command - empty string disables the use of the file command
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 briefwill result in 'file -i -b test_file'
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:
or
1 $mime =& new Mime_Types('/usr/local/apache/conf/mime.types');
1 $mime =& new Mime_Types(array(
2 'application/pdf' => 'pdf',
3 'application/postscript' => array('ai','eps')
4 ));
Get extension
Returns string containing a extension associated with $type
Example:
1 $ext = $mime->get_extension('application/postscript');
2 if ($ext) echo $ext;
Get extensions - returns array containing extension(s)
Example:
1 $exts = $mime->get_extensions('application/postscript');
2 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:
or
1 echo $mime->get_file_type('/path/to/my_file', false);
1 echo $mime->get_file_type('/path/to/my_file.gif');
default: true
Get type - returns MIME type based on the file extension
Example:
or
1 echo $mime->get_type('txt');
both examples above will return the same result.
1 echo $mime->get_type('test_file.txt');
Has extension
Returns true if extension $ext exists, false otherwise
Example:
1 if ($mime->has_extension('pdf')) echo 'Got it!';
Has type
Returns true if type $type exists, false otherwise
Example:
1 if ($mime->has_type('image/gif')) echo 'Got it!';
Load file containing mime types.
Example:
1 $result = $mime->load_file('/usr/local/apache/conf/mime.types');
2 echo (($result) ? 'Success!' : 'Failed');
Remove extension
Example:
or
1 $mime->remove_extension('txt');
or
1 $mime->remove_extension('txt exe html');
1 $mime->remove_extension(array('txt', 'exe', 'html'));
string holding extension(s) seperated by space, or array
Remove type
Example:
or
1 $mime->remove_type('text/plain');
or
1 $mime->remove_type('image/*');
2
1 $mime->remove_type();
2
if omitted, all types will be removed
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:
function name, or array holding an object and the method to call.
passed as the 3rd argument to $callback
Set
Set extension and MIME type
Example:
or
1 $mime->set('text/plain', 'txt');
or
1 $mime->set('text/html', array('html','htm'));
or
1 $mime->set('text/html', 'html htm');
1 $mime->set(array(
2 'application/pdf' => 'oda',
3 'application/postscript' => array('ai','eps')
4 ));
either array containing type and extensions, or the type as string
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