Namespaces

  • Nette
    • Application
      • Diagnostics
      • Responses
      • Routers
      • UI
    • Caching
      • Storages
    • ComponentModel
    • Config
      • Adapters
      • Extensions
    • Database
      • Diagnostics
      • Drivers
      • Reflection
      • Table
    • DI
      • Diagnostics
    • Diagnostics
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Latte
      • Macros
    • Loaders
    • Localization
    • Mail
    • Reflection
    • Security
      • Diagnostics
    • Templating
    • Utils
      • PhpGenerator
  • NetteModule
  • None
  • PHP

Classes

  • Arrays
  • Finder
  • Html
  • Json
  • LimitedScope
  • MimeTypeDetector
  • Neon
  • NeonEntity
  • Paginator
  • Strings
  • Tokenizer
  • Validators

Exceptions

  • AssertionException
  • JsonException
  • NeonException
  • RegexpException
  • TokenizerException
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
 1: <?php
 2: 
 3: /**
 4:  * This file is part of the Nette Framework (http://nette.org)
 5:  *
 6:  * Copyright (c) 2004 David Grudl (http://davidgrudl.com)
 7:  *
 8:  * For the full copyright and license information, please view
 9:  * the file license.txt that was distributed with this source code.
10:  */
11: 
12: namespace Nette\Utils;
13: 
14: use Nette;
15: 
16: 
17: 
18: /**
19:  * Mime type detector.
20:  *
21:  * @author     David Grudl
22:  */
23: final class MimeTypeDetector
24: {
25: 
26:     /**
27:      * Static class - cannot be instantiated.
28:      */
29:     final public function __construct()
30:     {
31:         throw new Nette\StaticClassException;
32:     }
33: 
34: 
35: 
36:     /**
37:      * Returns the MIME content type of file.
38:      * @param  string
39:      * @return string
40:      */
41:     public static function fromFile($file)
42:     {
43:         if (!is_file($file)) {
44:             throw new Nette\FileNotFoundException("File '$file' not found.");
45:         }
46: 
47:         $info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
48:         if (isset($info['mime'])) {
49:             return $info['mime'];
50: 
51:         } elseif (extension_loaded('fileinfo')) {
52:             $type = preg_replace('#[\s;].*\z#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
53: 
54:         } elseif (function_exists('mime_content_type')) {
55:             $type = mime_content_type($file);
56:         }
57: 
58:         return isset($type) && preg_match('#^\S+/\S+\z#', $type) ? $type : 'application/octet-stream';
59:     }
60: 
61: 
62: 
63:     /**
64:      * Returns the MIME content type of file.
65:      * @param  string
66:      * @return string
67:      */
68:     public static function fromString($data)
69:     {
70:         if (extension_loaded('fileinfo') && preg_match('#^(\S+/[^\s;]+)#', finfo_buffer(finfo_open(FILEINFO_MIME), $data), $m)) {
71:             return $m[1];
72: 
73:         } elseif (strncmp($data, "\xff\xd8", 2) === 0) {
74:             return 'image/jpeg';
75: 
76:         } elseif (strncmp($data, "\x89PNG", 4) === 0) {
77:             return 'image/png';
78: 
79:         } elseif (strncmp($data, "GIF", 3) === 0) {
80:             return 'image/gif';
81: 
82:         } else {
83:             return 'application/octet-stream';
84:         }
85:     }
86: 
87: }
88: 
Nette Framework 2.0.8 API API documentation generated by ApiGen 2.8.0