4
* This file is part of the Nette Framework (http://nette.org)
6
* Copyright (c) 2004 David Grudl (http://davidgrudl.com)
8
* For the full copyright and license information, please view
9
* the file license.txt that was distributed with this source code.
12
namespace Nette\Utils;
22
final class MimeTypeDetector
26
* Static class - cannot be instantiated.
28
final public function __construct()
30
throw new Nette\StaticClassException;
35
* Returns the MIME content type of file.
39
public static function fromFile($file)
41
if (!is_file($file)) {
42
throw new Nette\FileNotFoundException("File '$file' not found.");
45
$info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
46
if (isset($info['mime'])) {
49
} elseif (extension_loaded('fileinfo')) {
50
$type = preg_replace('#[\s;].*\z#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
52
} elseif (function_exists('mime_content_type')) {
53
$type = mime_content_type($file);
56
return isset($type) && preg_match('#^\S+/\S+\z#', $type) ? $type : 'application/octet-stream';
61
* Returns the MIME content type of file.
65
public static function fromString($data)
67
if (extension_loaded('fileinfo') && preg_match('#^(\S+/[^\s;]+)#', finfo_buffer(finfo_open(FILEINFO_MIME), $data), $m)) {
70
} elseif (strncmp($data, "\xff\xd8", 2) === 0) {
73
} elseif (strncmp($data, "\x89PNG", 4) === 0) {
76
} elseif (strncmp($data, "GIF", 3) === 0) {
80
return 'application/octet-stream';