HiveBrain v1.2.0
Get Started
← Back to all entries
patternphpMinor

PHP MIMEType class

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
phpclassmimetype

Problem

There isn't a very reliable way (in my opinion) to get the mime type of a specific extension, regardless of the file actually being of that type or not.

This type of thing usually is used when sending a different Content-type when you intend to send, for example, an image.

```
class MIMETypes {

/**
* @var string Contains the default type when none is found
* @access protected
*/
static private $default='application/octet-stream';

/**
* @var array Contains all the mime types and extensions
* @access protected
*/
static private $mimes=array(
'text'=>array(
'plain'=>array(
'php','php5','php4','php3','inc','sphp','phps','phtml',
'htm','html',
'txt'
)
),
'audio'=>array(
'aac'=>array('aac'),
'midi'=>array('mid','midi'),
'mp4'=>array('mp4','m4a'),
'mpeg'=>array('m2a','mp1','mp2','mp3','mp4','mpg','mpeg','mpa','mpga'),
'ogg'=>array('oga','ogg'),
'wav'=>array('wav','wave','pcm'),
'webm'=>array('webm'),
'x-mpequrl'=>array('m3u')
),
'video'=>array(
'mp4'=>array('mp4','m4v','m1v','m2v'),
'ogg'=>array('ogv'),
'webm'=>array('webm')
)
);

/**
* @var array Contains the last accessed contents and comes pre-initialized with some extensions and mime types that didn't matched
* @access protected
*/
static private $cache=array(
'ext'=>array(
'txt'=>array('plain/text')
),
'mime'=>array(
'/'=>array()
)
);

/**
* Creates a list of matching mime types for that file extension
* @param string $ext The extension to look for
* @param string|null $hint Hint to reduce the search load for only that mime "group"
WARNING*: Setting this parameter will ignore the cache!
* @ret

Solution

This is mostly a style review, but I'll be back to work on it further later.

Your usage of whitespace leaves a bit to be desired.

  • You should have whitespace in between your key => value, rather than none.



  • You should have spaces between commas



  • You should have spaces in between your binary operators static private $default='application/octet-stream'



  • You shouldn't have whitespace before and after your brackets



You should consider moving the regex statement in the following line to an external variable.

$ext = preg_replace( '@^(?:.*\.)*([^.\\]+)$@', '$1', $ext );

Code Snippets

$ext = preg_replace( '@^(?:.*\.)*([^.\\]+)$@', '$1', $ext );

Context

StackExchange Code Review Q#75512, answer score: 2

Revisions (0)

No revisions yet.