diff options
author | Brett Profitt <brett.profitt@gmail.com> | 2011-10-02 19:30:37 -0700 |
---|---|---|
committer | Brett Profitt <brett.profitt@gmail.com> | 2011-10-02 19:30:37 -0700 |
commit | f423684b5303b4196c8c728eb06cbd6eb9aad8b1 (patch) | |
tree | 5de8dfe913b2d43be0c713564fe9af70cc7c096e /engine/classes | |
parent | 19277946b643c7024c1aaad813f287bead695f21 (diff) | |
download | elgg-f423684b5303b4196c8c728eb06cbd6eb9aad8b1.tar.gz elgg-f423684b5303b4196c8c728eb06cbd6eb9aad8b1.tar.bz2 |
Merged file mime type detection from 1.7.
Diffstat (limited to 'engine/classes')
-rw-r--r-- | engine/classes/ElggFile.php | 41 |
1 files changed, 41 insertions, 0 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. |