From d6e4880ee6971663466c7cb08f2a42b094df6b87 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Fri, 20 Aug 2010 16:25:31 +0000 Subject: [PATCH] Enhance mime detection --- htdocs/lib/files.lib.php | 22 ++++++++++++++++++---- 1 file changed, 18 insertions(+), 4 deletions(-) diff --git a/htdocs/lib/files.lib.php b/htdocs/lib/files.lib.php index 4691c159424..ae42eb2e814 100644 --- a/htdocs/lib/files.lib.php +++ b/htdocs/lib/files.lib.php @@ -162,12 +162,14 @@ function dol_compare_file($a, $b) * Return mime type of a file * @param file Filename * @param default Default mime type if extension not found in known list + * @param shortmime Return short mime, otherwise full mime string * @return string Return a mime type family * (text/xxx, application/xxx, image/xxx, audio, video, archive) */ -function dol_mimetype($file,$default='application/octet-stream') +function dol_mimetype($file,$default='application/octet-stream',$shortmime=0) { $mime=$default; + // Text files if (preg_match('/\.txt$/i',$file)) $mime='text/plain'; if (preg_match('/\.rtx$/i',$file)) $mime='text/richtext'; @@ -205,8 +207,8 @@ function dol_mimetype($file,$default='application/octet-stream') // Scripts if (preg_match('/\.bat$/i',$file)) $mime='text/x-bat'; if (preg_match('/\.sh$/i',$file)) $mime='text/x-sh'; - if (preg_match('/\.ksh$/i',$file)) $mime='text/x-ksh'; - if (preg_match('/\.bash$/i',$file)) $mime='text/x-bash'; + if (preg_match('/\.ksh$/i',$file)) $mime='text/x-ksh'; + if (preg_match('/\.bash$/i',$file)) $mime='text/x-bash'; // Images if (preg_match('/\.ico$/i',$file)) $mime='image/x-icon'; if (preg_match('/\.(jpg|jpeg)$/i',$file)) $mime='image/jpeg'; @@ -225,8 +227,20 @@ function dol_mimetype($file,$default='application/octet-stream') if (preg_match('/\.(avi|divx|xvid|wmv|mpg|mpeg)$/i',$file)) $mime='video'; // Archive if (preg_match('/\.(zip|rar|gz|tgz|z|cab|bz2|7z|tar|lzh)$/i',$file)) $mime='archive'; // application/xxx where zzz is zip, ... + // Exe + if (preg_match('/\.(exe|com)$/i',$file)) $mime='application'; + // Exe + if (preg_match('/\.(dll|lib|o|so|a)$/i',$file)) $mime='library'; - return $mime; + if ($shortmime) + { + $tmp=explode('/',$mime); + return $tmp[1]; + } + else + { + return $mime; + } }