Namespaces

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

Classes

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

Exceptions

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