php

到底什么是mime类型,他的中文意思是什么

解决方案 »

  1.   

    001
    <?php
    002
    $mime = array (
    003
            //applications
    004
            'ai'    => 'application/postscript',
    005
            'eps'   => 'application/postscript',
    006
            'exe'   => 'application/octet-stream',
    007
            'doc'   => 'application/vnd.ms-word',
    008
            'xls'   => 'application/vnd.ms-excel',
    009
            'ppt'   => 'application/vnd.ms-powerpoint',
    010
            'pps'   => 'application/vnd.ms-powerpoint',
    011
            'pdf'   => 'application/pdf',
    012
            'xml'   => 'application/xml',
    013
            'odt'   => 'application/vnd.oasis.opendocument.text',
    014
            'swf'   => 'application/x-shockwave-flash',
    015
            // archives
    016
            'gz'    => 'application/x-gzip',
    017
            'tgz'   => 'application/x-gzip',
    018
            'bz'    => 'application/x-bzip2',
    019
            'bz2'   => 'application/x-bzip2',
    020
            'tbz'   => 'application/x-bzip2',
    021
            'zip'   => 'application/zip',
    022
            'rar'   => 'application/x-rar',
    023
            'tar'   => 'application/x-tar',
    024
            '7z'    => 'application/x-7z-compressed',
    025
            // texts
    026
            'txt'   => 'text/plain',
    027
            'php'   => 'text/x-php',
    028
            'html'  => 'text/html',
    029
            'htm'   => 'text/html',
    030
            'js'    => 'text/javascript',
    031
            'css'   => 'text/css',
    032
            'rtf'   => 'text/rtf',
    033
            'rtfd'  => 'text/rtfd',
    034
            'py'    => 'text/x-python',
    035
            'java'  => 'text/x-java-source',
    036
            'rb'    => 'text/x-ruby',
    037
            'sh'    => 'text/x-shellscript',
    038
            'pl'    => 'text/x-perl',
    039
            'sql'   => 'text/x-sql',
    040
            // images
    041
            'bmp'   => 'image/x-ms-bmp',
    042
            'jpg'   => 'image/jpeg',
    043
            'jpeg'  => 'image/jpeg',
    044
            'gif'   => 'image/gif',
    045
            'png'   => 'image/png',
    046
            'tif'   => 'image/tiff',
    047
            'tiff'  => 'image/tiff',
    048
            'tga'   => 'image/x-targa',
    049
            'psd'   => 'image/vnd.adobe.photoshop',
    050
            //audio
    051
            'mp3'   => 'audio/mpeg',
    052
            'mid'   => 'audio/midi',
    053
            'ogg'   => 'audio/ogg',
    054
            'mp4a'  => 'audio/mp4',
    055
            'wav'   => 'audio/wav',
    056
            'wma'   => 'audio/x-ms-wma',
    057
            // video
    058
            'avi'   => 'video/x-msvideo',
    059
            'dv'    => 'video/x-dv',
    060
            'mp4'   => 'video/mp4',
    061
            'mpeg'  => 'video/mpeg',
    062
            'mpg'   => 'video/mpeg',
    063
            'mov'   => 'video/quicktime',
    064
            'wm'    => 'video/x-ms-wmv',
    065
            'flv'   => 'video/x-flv',
    066
            'mkv'   => 'video/x-matroska'
    067
            );
    068
     
    069
    function _getMimeDetect() {
    070
        if (class_exists('finfo')) {
    071
            return 'finfo';
    072
        } else if (function_exists('mime_content_type')) {
    073
            return 'mime_content_type';
    074
        } else if ( function_exists('exec')) {
    075
            $result = exec('file -ib '.escapeshellarg(__FILE__));
    076
            if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
    077
                return 'linux';
    078
            }
    079
            $result = exec('file -Ib '.escapeshellarg(__FILE__));
    080
            if ( 0 === strpos($result, 'text/x-php') OR 0 === strpos($result, 'text/x-c++')) {
    081
                return 'bsd';
    082
            }
    083
        }
    084
        return 'internal';
    085
    }
    086
     
    087
    function _getMimeType($path) {
    088
        global $mime;
    089
        $fmime = _getMimeDetect();
    090
        switch($fmime) {
    091
            case 'finfo':
    092
                $finfo = finfo_open(FILEINFO_MIME);
    093
                if ($finfo)
    094
                    $type = @finfo_file($finfo, $path);
    095
                break;
    096
            case 'mime_content_type':
    097
                $type = mime_content_type($path);
    098
                break;
    099
            case 'linux':
    100
                $type = exec('file -ib '.escapeshellarg($path));
    101
                break;
    102
            case 'bsd':
    103
                $type = exec('file -Ib '.escapeshellarg($path));
    104
                break;
    105
            default:
    106
                $pinfo = pathinfo($path);
    107
                $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
    108
                $type = isset($mime[$ext]) ? $mime[$ext] : 'unkown';
    109
                break;
    110
        }
    111
        $type = explode(';', $type);
    112
         
    113
        //需要加上这段,因为如果使用mime_content_type函数来获取一个不存在的$path时会返回'application/octet-stream'
    114
        if ($fmime != 'internal' AND $type[0] == 'application/octet-stream') {
    115
            $pinfo = pathinfo($path);
    116
            $ext = isset($pinfo['extension']) ? strtolower($pinfo['extension']) : '';
    117
            if (!empty($ext) AND !empty($mime[$ext])) {
    118
                $type[0] = $mime[$ext];
    119
            }
    120
        }
    121
         
    122
        return $type[0];
    123
    }
    124
     
    125
    $path = '1.txt';  //实际上当前路径并不存在1.txt
    126
    var_dump(_getMimeType($path));
    127
     
    128
    /*End of php*/
      

  2.   

    http://baike.baidu.com/view/160611.htm