From ad74fe66da948c8b81b18398a6dbd9583d3741f5 Mon Sep 17 00:00:00 2001 From: Adrian Lang Date: Tue, 22 Jun 2010 17:53:16 +0200 Subject: Rewrite mimetype (faster, shorter, no PHP Notice) --- inc/confutils.php | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'inc') diff --git a/inc/confutils.php b/inc/confutils.php index 0c1c0c64a..4306dab8f 100644 --- a/inc/confutils.php +++ b/inc/confutils.php @@ -16,29 +16,24 @@ * @author Andreas Gohr */ 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); + } } /** -- cgit v1.2.3