aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--engine/classes/ElggFile.php41
-rw-r--r--mod/file/actions/file/upload.php7
2 files changed, 45 insertions, 3 deletions
diff --git a/engine/classes/ElggFile.php b/engine/classes/ElggFile.php
index fe25491a8..f21621ffd 100644
--- a/engine/classes/ElggFile.php
+++ b/engine/classes/ElggFile.php
@@ -121,6 +121,47 @@ class ElggFile extends ElggObject {
}
/**
+ * Detects mime types based on filename or actual file.
+ *
+ * @param mixed $file The full path of the file to check. For uploaded files, use tmp_name.
+ * @param mixed $default A default. Useful to pass what the browser thinks it is.
+ * @since 1.7.12
+ *
+ * @return mixed Detected type on success, false on failure.
+ */
+ static function detectMimeType($file = null, $default = null) {
+ if (!$file) {
+ if (isset($this) && $this->filename) {
+ $file = $this->filename;
+ } else {
+ return false;
+ }
+ }
+
+ $mime = false;
+
+ // for PHP5 folks.
+ if (function_exists('finfo_file') && defined('FILEINFO_MIME_TYPE')) {
+ $resource = finfo_open(FILEINFO_MIME_TYPE);
+ if ($resource) {
+ $mime = finfo_file($resource, $file);
+ }
+ }
+
+ // for everyone else.
+ if (!$mime && function_exists('mime_content_type')) {
+ $mime = mime_content_type($file);
+ }
+
+ // default
+ if (!$mime) {
+ return $default;
+ }
+
+ return $mime;
+ }
+
+ /**
* Set the optional file description.
*
* @param string $description The description.
diff --git a/mod/file/actions/file/upload.php b/mod/file/actions/file/upload.php
index 88a01745d..ee2889b4c 100644
--- a/mod/file/actions/file/upload.php
+++ b/mod/file/actions/file/upload.php
@@ -90,10 +90,11 @@ if (isset($_FILES['upload']['name']) && !empty($_FILES['upload']['name'])) {
$filestorename = elgg_strtolower(time().$_FILES['upload']['name']);
}
- $file->setFilename($prefix.$filestorename);
- $file->setMimeType($_FILES['upload']['type']);
+ $mime_type = $file->detectMimeType($_FILES['upload']['tmp_name'], $_FILES['upload']['type']);
+ $file->setFilename($prefix . $filestorename);
+ $file->setMimeType($mime_type);
$file->originalfilename = $_FILES['upload']['name'];
- $file->simpletype = file_get_simple_type($_FILES['upload']['type']);
+ $file->simpletype = file_get_simple_type($mime_type);
// Open the file to guarantee the directory exists
$file->open("write");