| | 13 | |
| | 14 | /** |
| | 15 | * Get the extension of a filename. |
| | 16 | * |
| | 17 | * @param string filename |
| | 18 | * @return string |
| | 19 | */ |
| | 20 | public static function extension($filename) |
| | 21 | { |
| | 22 | // Return the extension of the filename |
| | 23 | return preg_replace('/^.+\.(.+?)$/', '$1', $filename); |
| | 24 | } |
| | 25 | |
| | 26 | /** |
| | 27 | * Attempt to get the mime type from a file. This method is horribly |
| | 28 | * unreliable, due to PHP being horribly unreliable when it comes to |
| | 29 | * determining the mime-type of a file. |
| | 30 | * |
| | 31 | * @param string filename |
| | 32 | * @return string mime-type, if found |
| | 33 | * @return boolean FALSE, if not found |
| | 34 | */ |
| | 35 | public static function mime($filename) |
| | 36 | { |
| | 37 | // Make sure the file is readable |
| | 38 | if ( ! file_exists($filename) OR ! is_file($filename) OR ! is_readable($filename)) |
| | 39 | return FALSE; |
| | 40 | |
| | 41 | // Get the extension from the filename |
| | 42 | $extension = pathinfo($filename, PATHINFO_EXTENSION); |
| | 43 | |
| | 44 | if (preg_match('/^jpe?g|png|[gt]if|bmp|swf$/', $extension)) |
| | 45 | { |
| | 46 | // Disable error reporting |
| | 47 | $ER = error_reporting(0); |
| | 48 | |
| | 49 | // Use getimagesize() to find the mime type on images |
| | 50 | $mime = getimagesize($filename); |
| | 51 | |
| | 52 | // Turn error reporting back on |
| | 53 | error_reporting($ER); |
| | 54 | |
| | 55 | // Return the mime type |
| | 56 | if (isset($mime['mime'])) |
| | 57 | return $mime['mime']; |
| | 58 | } |
| | 59 | |
| | 60 | if (function_exists('finfo_open')) |
| | 61 | { |
| | 62 | // Use the fileinfo extension |
| | 63 | $finfo = finfo_open(FILEINFO_MIME); |
| | 64 | $mime = finfo_file($finfo, $filename); |
| | 65 | finfo_close($finfo); |
| | 66 | |
| | 67 | // Return the mime type |
| | 68 | return $mime; |
| | 69 | } |
| | 70 | |
| | 71 | if (ini_get('mime_magic.magicfile') AND function_exists('mime_content_type')) |
| | 72 | { |
| | 73 | // Return the mime type using mime_content_type |
| | 74 | return mime_content_type($filename); |
| | 75 | } |
| | 76 | |
| | 77 | if ( ! empty($extension) AND is_array($mime = Config::item('mimes.'.$extension))) |
| | 78 | { |
| | 79 | // Return the mime-type guess, based on the extension |
| | 80 | return $mime[0]; |
| | 81 | } |
| | 82 | |
| | 83 | // Unable to find the mime-type |
| | 84 | return FALSE; |
| | 85 | } |