1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 
	<?php
namespace ICanBoogie\HTTP;
class FileInfo
{
    static public $types = [
        '.doc'  => 'application/msword',
        '.docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
        '.gif'  => 'image/gif',
        '.jpg'  => 'image/jpeg',
        '.jpeg' => 'image/jpeg',
        '.js'   => 'application/javascript',
        '.json' => 'application/json',
        '.mp3'  => 'audio/mpeg',
        '.odt'  => 'application/vnd.oasis.opendocument.text',
        '.pdf'  => 'application/pdf',
        '.php'  => 'application/x-php',
        '.png'  => 'image/png',
        '.psd'  => 'application/psd',
        '.rar'  => 'application/rar',
        '.txt'  => 'text/plain',
        '.zip'  => 'application/zip',
        '.xls'  => 'application/vnd.ms-excel',
        '.xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'
    ];
    static public $forced_types = [
        '.js',
        '.json',
        '.php',
        '.txt'
    ];
    static public $types_alias = [
        'text/x-php' => 'application/x-php'
    ];
    
    static public function resolve_type($pathname, &$extension = null)
    {
        $extension = '.' . strtolower(pathinfo($pathname, PATHINFO_EXTENSION));
        if (in_array($extension, self::$forced_types))
        {
            return self::$types[$extension];
        }
        if (file_exists($pathname) && extension_loaded('fileinfo'))
        {
            $fi = new \finfo(FILEINFO_MIME_TYPE);
            $type = $fi->file($pathname);
            if ($type)
            {
                return isset(self::$types_alias[$type]) ? self::$types_alias[$type] : $type;
            }
        } 
        if (isset(self::$types[$extension]))
        {
            return self::$types[$extension];
        }
        return 'application/octet-stream'; 
    }
}