我想根据php读取的文件数据流来判断文件的mime-type,怎么实现呢,有高手吗?比如:$stream = file_get_contents($fileName);
如何根据$stream的内容判断mime-type呢.
不要用mime_content_type方法, 因为这个方法好像在php5.3下不怎么好用.
也不要用根据文件名辍返回的方法,因为这种方法不怎么可靠.

解决方案 »

  1.   

    file_get_contents 能返回文件流吗,它返回是的字符串。
      

  2.   

    鉴于"hzqghost"的回复,我把头一句改一下,改成这样:"我想根据php读取的文件内容判断文件的mime-type,怎么实现呢"
      

  3.   

    参考
    http://topic.csdn.net/u/20110301/11/8e2153e2-5903-47a5-ae55-5612cf6084c1.html
    这里有你说的。
    curl
      

  4.   


    header("content-type:text/html;charset=gbk");
    $mime = array(
        'gif' => 'image/gif',
        'jpg' => 'image/jpeg',
        'jpeg' => 'image/jpeg',
        'jpe' => 'image/jpeg',
        'bmp' => 'image/bmp',
        'png' => 'image/png',
        'tif' => 'image/tiff',
        'tiff' => 'image/tiff',
        'pict' => 'image/x-pict',
        'pic' => 'image/x-pict',
        'pct' => 'image/x-pict',
        'tif' => 'image/tiff',
        'tiff' => 'image/tiff',
        'psd' => 'image/x-photoshop',    'swf' => 'application/x-shockwave-flash',
        'js' => 'application/x-javascrīpt',
        'pdf' => 'application/pdf',
        'ps' => 'application/postscrīpt',
        'eps' => 'application/postscrīpt',
        'ai' => 'application/postscrīpt',
        'wmf' => 'application/x-msmetafile',    'css' => 'text/css',
        'htm' => 'text/html',
        'html' => 'text/html',
        'txt' => 'text/plain',
        'xml' => 'text/xml',
        'wml' => 'text/wml',
        'wbmp' => 'image/vnd.wap.wbmp',    'mid' => 'audio/midi',
        'wav' => 'audio/wav',
        'mp3' => 'audio/mpeg',
        'mp2' => 'audio/mpeg',    'avi' => 'video/x-msvideo',
        'mpeg' => 'video/mpeg',
        'mpg' => 'video/mpeg',
        'qt' => 'video/quicktime',
        'mov' => 'video/quicktime',    'lha' => 'application/x-lha',
        'lzh' => 'application/x-lha',
        'z' => 'application/x-compress',
        'gtar' => 'application/x-gtar',
        'gz' => 'application/x-gzip',
        'gzip' => 'application/x-gzip',
        'tgz' => 'application/x-gzip',
        'tar' => 'application/x-tar',
        'bz2' => 'application/bzip2',
        'zip' => 'application/zip',
        'arj' => 'application/x-arj',
        'rar' => 'application/x-rar-compressed',    'hqx' => 'application/mac-binhex40',
        'sit' => 'application/x-stuffit',
        'bin' => 'application/x-macbinary',    'uu' => 'text/x-uuencode',
        'uue' => 'text/x-uuencode',    'latex'=> 'application/x-latex',
        'ltx' => 'application/x-latex',
        'tcl' => 'application/x-tcl',    'pgp' => 'application/pgp',
        'asc' => 'application/pgp',
        'exe' => 'application/x-msdownload',
        'doc' => 'application/msword',
        'rtf' => 'application/rtf',
        'xls' => 'application/vnd.ms-excel',
        'ppt' => 'application/vnd.ms-powerpoint',
        'mdb' => 'application/x-msaccess',
        'wri' => 'application/x-mswrite',
    );
    $url = 'http://tp4.sinaimg.cn/1692980395/180/1298526784/0';
    //$url = 'http://www.baidu.com/';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_HEADER, 1);
    curl_setopt($ch, CURLOPT_NOBODY, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    $contents = curl_exec($ch);
    curl_close($ch);if(preg_match('/Content-Type:\s+(.*?)(?:;|\s)/i',$contents,$arr)){
      $mimestr = strtolower(trim($arr[1]));
      $outkey = array_keys($mime,$mimestr);
      echo '文件类型是:'.implode(',',$outkey) ;
    }else{
      echo '没有匹配到类型';
    }
      

  5.   

    curl不能访问地文件,当然可能模拟'http'方式来访问本地文件,但curl貌似是根据文件名后辍来判断的,比如,我把gif文件的后辍改为".rar"之后得到的结果就是"Content-Type: application/x-rar-compressed".getimagesize()只能判断图片文件,也差强人意吧.谢谢回复!
      

  6.   

    服务器本地可用这个:
    $f='cookie.txt';
    $path_parts = pathinfo($f);
    echo $path_parts['extension'], "\n"; //这个是扩展名
    ========================================
    比如,我把gif文件的后辍改为".rar"之后得到的结果就是"Content-Type: application/x-rar-compressed".
    这个除了getimagesize()能读出图片后缀改.rar的,其他的都读不出来。
    ========================================
    客户端的,只能用js实现。
      

  7.   

    $file = fopen($filename, "rb");
    $bin = fread($file, 2); //只读2字节
    fclose($file);
    $strInfo = @unpack("c2chars", $bin);
    $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
    $fileType = '';
    switch ($typeCode)
    {
    case 7790:
    $fileType = 'exe';
    break;
    case 7784:
    $fileType = 'midi';
    break;
    case 8297:
    $fileType = 'rar';
    break;
    case 255216:
    $fileType = 'jpg';
    break;
    case 7173:
    $fileType = 'gif';
    break;
    case 6677:
    $fileType = 'bmp';
    break;
    case 13780:
    $fileType = 'png';
    break;
    default:
    echo 'unknown';
    }
    echo 'this is a(an) '.$fileType.' file:'.$typeCode; 
      

  8.   

    http://www.cnblogs.com/chengmo/archive/2010/10/17/1853710.html
    是说的这个吧,去文件头信息,得到的是最标准的文件格式,改扩展名也是白搭的
      

  9.   

    这个可以。不过好象有些JPG的文件他会识别为GIF!
      

  10.   

    不好意思,几天不上来了,感谢大家的热心帮助,经过测试发现10楼"zhang6464"提供的方法比较好使, 不过也发现了一个小小的问题,就是"array("FFD8FFE1","jpg")"里应该是"array("FFD8FFE0","jpg")"吧, 反正我打开的jpg是"FFD8FFE0",测试之后也发现,改为"FFD8FFE0"才正确.