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

Classes

Exceptions

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