FileExt.class.php 6.34 KB
<?php
class Util_Common_Io_FileExt
{
	private  static $mExtCode = null;
	private  static $mExtMine = null;
	
	/**
	 * 获取扩展
	 * 
	 * @param string $fileName
	 * @return ext
	 */
	public  static function getExt($fileName)
	{
	    self::init();
		//文件扩展编码
		$fileExtCode = '';
		if(file_exists($fileName))
		{
    		if($handle = @fopen($fileName,'rb'))
    		{ 
        		//轻便的咨询文件锁定,设置独占锁定   
       	 		flock($handle,LOCK_SH);
       	 		//读取头信息高位
        		$fileExtCode = fread($handle, 2);
        		fclose($handle); 
        		if(strlen($fileExtCode))
        		{
        			$fileExtCode = unpack('C2char',$fileExtCode);
					$fileExtCode = intval(implode('',$fileExtCode));	
        		}
   			} 
		}
		return array_search($fileExtCode, self::$mExtCode);
	}
	
	/**
	 * 根据获取扩展
	 * 
	 * @param string $code
	 */
	public static function getExtByCode($code)
	{
		self::init();
		return array_search($code, self::$mExtCode);
	}
	
	/**
	 * 获取minetype的扩展名
	 * 
	 * @param string $mineType
	 * @param string $extName
	 * @return array
	 */
	public static function getMineExts($mineType)
	{
	    self::init();
		if(array_key_exists($mineType,self::$mExtMine))
		{
			return self::$mExtMine[$mineType];
		}
		else
		{
			return array();
        }
			
	}
	
    /**
     * 
     * @param unknown_type $ext
     * @return unknown|string
     */
	public static function getMine($ext)
	{
	    self::init();
		$mine = '';
		if(strlen($ext))
		{
		     foreach(self::$mExtMine as $key=>$value)
			 {
				if(array_search($ext, $value)!==FALSE)
				 {
					$mine = $key;
					break;
				 }
			 
			 }
		
		}
		if(strlen($mine))
		{
			return $mine;
		}
		else
		{
			return 'application/octet-stream';
		}
	
	}
	
	/**
     * 初始化
     */
    private static function init()
    {
        if(!isset(self::$mExtCode)) {
             self::$mExtCode = array(
                'gif'       =>7173,
                'bmp'       =>6677,
                'tif'       =>7373,
                'png'       =>13780,
                'jpg'       =>255216,
                'dwg'       =>6567,
                'psd'       =>5666,
                'rtf'       =>12392,
                'eml'       =>68101,
                'dbx'       =>-49,
                'pst'       =>3366,
                'mdb'       =>83116,
                'eps/ps'    =>3733,
                'pwl'       =>-29,
                'wpd'       =>-187,
                'exe'       =>7790,
                'rar'       =>8297,
                'xlsx/docx/pptx/zip'=>8075,//信息头一样
                'xml'       =>6063,
                'html'      =>6033,
                'aspx'      =>239187,
                'cs'        =>117115,
                'js'        =>119105,
                'txt'       =>210187,
                'sql'       =>255254,
                'pdf'       =>3780,
                'xls/doc/ppt'=>-48, //三种信息头一样
                'wav'       =>8765,
                'avi'       =>6586,
                'ram'       =>46114,
                'rm'        =>4682,
                'mov'       =>109111,
                'asf'       =>4838,
                'mid'       =>7784,
                'swf'       =>6787,
                'chm'       =>7384,
                'gz'        =>31,
            );
        }
        if(!isset(self::$mExtMine)) {
             self::$mExtMine = array(
                'video/x-msvideo'               => array('avi'),
                'audio/x-aiff'                  => array('aif','aiff','aifc'),
                'audio/basic'                   => array('au','snd'),
                'application/postscript'        => array('ai','eps','ps'),
                'application/astound'           => array('asd','asn'),
                'application/octet-stream'      => array('rar','bin','exe','com','dll','class'),
                'application/x-macbinary'       => array('bin'),
                'text/comma-separated-values'   => array('csv'),
                'text/css'                      => array('css'),
                'application/acad'              => array('dwg'),
                'application/dxf'               => array('dxf'),
                'application/x-director'        => array('dcr','dir','dxr'),
                'application/msword'            => array('doc','dot'),
                'application/vnd.openxmlformats-officedocument.wordprocessingml.document'=>array('docx'),
                'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'=>array('xlsx'),
                'application/vnd.openxmlformats-officedocument.presentationml.presentation'=>array('pptx'),
                'application/gzip'              => array('gz'),
                'image/gif'                     => array('gif'),
                'application/mshelp'            => array('hlp','chm'),
                'text/html'                     => array('htm','html','shtml'),
                'text/xml'                      => array('xml'),
                'text/javascript'               => array('js'),
                'image/jpeg'                    => array('jpeg','jpg','jpe'),
                'image/png'                     => array('png'),
                'image/bmp'                     => array('bmp'),
                'video/mpeg'                    => array('mpeg','mpg','mpe'),
                'audio/mpeg'                    => array('mp3'),
                'audio/mp4'                     => array('m4a'),
             	'audio/amr'                     =>  array('amr'),
                'video/quicktime'               => array('qt','mov'),
                'application/msaccess'          => array('mdb'),
                'application/mspowerpoint'      => array('ppt','ppz','pps','pot'),
                'application/pdf'               => array('pdf'),
                'application/rtf'               => array('rtf'),
                'text/richtext'                 => array('rtx'),
                'audio/x-pn-realaudio'          => array('ram','rm'),
                'application/vnd.rn-realmedia'  => array('rm','rmvb'),
                'application/x-shockwave-flash' => array('swf','cab'),
                'text/plain'                    => array('txt'),
                'application/msexcel'           => array('xls','xla'),
                'application/x-compress'        => array('z'),
                'application/zip'   => array('zip'),
            );  
        }
    
    }
    
}

?>