From 35e07cccb4f80b5e0af021aadc4b3c0c59517e05 Mon Sep 17 00:00:00 2001 From: Marcel Werk Date: Mon, 13 Feb 2012 12:18:08 +0100 Subject: [PATCH] Using finfo to get the mime type of uploaded files --- .../lib/system/upload/UploadHandler.class.php | 5 +++-- .../install/files/lib/util/FileUtil.class.php | 21 +++++++++++++++++++ 2 files changed, 24 insertions(+), 2 deletions(-) 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); + } } -- 2.20.1