From: Marcel Werk Date: Mon, 13 Feb 2012 11:18:08 +0000 (+0100) Subject: Using finfo to get the mime type of uploaded files X-Git-Tag: 2.0.0_Beta_1~1356 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=35e07cccb4f80b5e0af021aadc4b3c0c59517e05;p=GitHub%2FWoltLab%2FWCF.git Using finfo to get the mime type of uploaded files --- diff --git a/wcfsetup/install/files/lib/system/upload/UploadHandler.class.php b/wcfsetup/install/files/lib/system/upload/UploadHandler.class.php index afdf62aa5b..73911764c5 100644 --- a/wcfsetup/install/files/lib/system/upload/UploadHandler.class.php +++ b/wcfsetup/install/files/lib/system/upload/UploadHandler.class.php @@ -1,5 +1,6 @@ files[] = new UploadFile($rawFileData['name'][$i], $rawFileData['tmp_name'][$i], $rawFileData['size'][$i], $rawFileData['error'][$i], $rawFileData['type'][$i]); + $this->files[] = new UploadFile($rawFileData['name'][$i], $rawFileData['tmp_name'][$i], $rawFileData['size'][$i], $rawFileData['error'][$i], (FileUtil::getMimeType($rawFileData['tmp_name'][$i]) ?: $rawFileData['type'][$i])); } } else { - $this->files[] = new UploadFile($rawFileData['name'], $rawFileData['tmp_name'], $rawFileData['size'], $rawFileData['error'], $rawFileData['type']); + $this->files[] = new UploadFile($rawFileData['name'], $rawFileData['tmp_name'], $rawFileData['size'], $rawFileData['error'], (FileUtil::getMimeType($rawFileData['tmp_name']) ?: $rawFileData['type'])); } } diff --git a/wcfsetup/install/files/lib/util/FileUtil.class.php b/wcfsetup/install/files/lib/util/FileUtil.class.php index 6164f8fee6..eb27e610d3 100644 --- a/wcfsetup/install/files/lib/util/FileUtil.class.php +++ b/wcfsetup/install/files/lib/util/FileUtil.class.php @@ -17,6 +17,12 @@ use wcf\system\WCF; * @category Community Framework */ class FileUtil { + /** + * finfo instance + * @var \finfo + */ + protected static $finfo = null; + /** * Tries to find the temp folder. * @@ -600,4 +606,19 @@ class FileUtil { public static function isApacheModule() { return function_exists('apache_get_version'); } + + /** + * Returns the mime type of a file. + * + * @param string $filename + * @return string + */ + public static function getMimeType($filename) { + if (self::$finfo === null) { + if (!class_exists('\finfo', false)) return ''; + self::$finfo = new \finfo(FILEINFO_MIME_TYPE); + } + + return self::$finfo->file($filename); + } }