Packages

  • 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

  • NArrays
  • NFinder
  • NHtml
  • NJson
  • NLimitedScope
  • NMimeTypeDetector
  • NNeon
  • NNeonEntity
  • NPaginator
  • NStrings
  • NTokenizer
  • NValidators

Exceptions

  • NAssertionException
  • NJsonException
  • NNeonException
  • NRegexpException
  • NTokenizerException
  • Overview
  • Package
  • 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:  * @package Nette\Utils
11:  */
12: 
13: 
14: 
15: /**
16:  * Mime type detector.
17:  *
18:  * @author     David Grudl
19:  * @package Nette\Utils
20:  */
21: final class NMimeTypeDetector
22: {
23: 
24:     /**
25:      * Static class - cannot be instantiated.
26:      */
27:     final public function __construct()
28:     {
29:         throw new NStaticClassException;
30:     }
31: 
32: 
33:     /**
34:      * Returns the MIME content type of file.
35:      * @param  string
36:      * @return string
37:      */
38:     public static function fromFile($file)
39:     {
40:         if (!is_file($file)) {
41:             throw new FileNotFoundException("File '$file' not found.");
42:         }
43: 
44:         $info = @getimagesize($file); // @ - files smaller than 12 bytes causes read error
45:         if (isset($info['mime'])) {
46:             return $info['mime'];
47: 
48:         } elseif (extension_loaded('fileinfo')) {
49:             $type = preg_replace('#[\s;].*\z#', '', finfo_file(finfo_open(FILEINFO_MIME), $file));
50: 
51:         } elseif (function_exists('mime_content_type')) {
52:             $type = mime_content_type($file);
53:         }
54: 
55:         return isset($type) && preg_match('#^\S+/\S+\z#', $type) ? $type : 'application/octet-stream';
56:     }
57: 
58: 
59:     /**
60:      * Returns the MIME content type of file.
61:      * @param  string
62:      * @return string
63:      */
64:     public static function fromString($data)
65:     {
66:         if (extension_loaded('fileinfo') && preg_match('#^(\S+/[^\s;]+)#', finfo_buffer(finfo_open(FILEINFO_MIME), $data), $m)) {
67:             return $m[1];
68: 
69:         } elseif (strncmp($data, "\xff\xd8", 2) === 0) {
70:             return 'image/jpeg';
71: 
72:         } elseif (strncmp($data, "\x89PNG", 4) === 0) {
73:             return 'image/png';
74: 
75:         } elseif (strncmp($data, "GIF", 3) === 0) {
76:             return 'image/gif';
77: 
78:         } else {
79:             return 'application/octet-stream';
80:         }
81:     }
82: 
83: }
84: 
Nette Framework 2.0.12 (for PHP 5.2, prefixed) API API documentation generated by ApiGen 2.8.0