Skip to content
Snippets Groups Projects
Commit ad74fe66 authored by Adrian Lang's avatar Adrian Lang
Browse files

Rewrite mimetype (faster, shorter, no PHP Notice)

parent 7e7f15ca
No related branches found
No related tags found
No related merge requests found
......@@ -16,29 +16,24 @@
* @author Andreas Gohr <andi@splitbrain.org>
*/
function mimetype($file, $knownonly=true){
$ret = array(false,false,false); // return array
$mtypes = getMimeTypes(); // known mimetypes
$exts = join('|',array_keys($mtypes)); // known extensions (regexp)
if(!$knownonly){
$exts = $exts.'|[_\-A-Za-z0-9]+'; // any extension
$ext = strrpos($file, '.');
if ($ext === false) {
return array(false, false, false);
}
if(preg_match('#\.('.$exts.')$#i',$file,$matches)){
$ext = strtolower($matches[1]);
}
if($ext){
if (isset($mtypes[$ext])){
if($mtypes[$ext][0] == '!'){
$ret = array($ext, substr($mtypes[$ext],1), true);
}else{
$ret = array($ext, $mtypes[$ext], false);
}
}elseif(!$knownonly){
$ret = array($ext, 'application/octet-stream', true);
$ext = strtolower(substr($file, $ext + 1));
if (!isset($mtypes[$ext])){
if ($knownonly) {
return array(false, false, false);
} else {
return array($ext, 'application/octet-stream', true);
}
}
return $ret;
if($mtypes[$ext][0] == '!'){
return array($ext, substr($mtypes[$ext],1), true);
}else{
return array($ext, $mtypes[$ext], false);
}
}
/**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment