河汉清且浅,牵牛敦而纯

 

 

 

通过文件头来判断文件类型 

https://www.garykessler.net/library/file_sigs.html

https://en.wikipedia.org/wiki/List_of_file_signatures

https://gist.github.com/Qti3e/6341245314bf3513abb080677cd1c93b

https://gist.github.com/k1ic/ad805d5e3ee9931bea2c

https://gist.github.com/ldong/f334b9dcd421c99e094d

https://gist.github.com/soasme/6085733



php文件格式(mime类型)对照表
https://blog.csdn.net/q343509740/article/details/79616603

1.  mime_content_type返回指定文件的MIME类型,用法:

echo mime_content_type ( 'php.gif' ) . "\n" ;
echo mime_content_type ( 'test.php' );

2.  Fileinfo 获取文件MIME类型(finfo_open)

PHP官方推荐mime_content_type()的替代函数是Fileinfo函数。用法:

$finfo = finfo_open(FILEINFO_MIME);
$mimetype = finfo_file($finfo, $filename);
finfo_close($finfo);

function getTypeList(){
    return array(
        "jpg"  =>    "FFD8FFE1",
        "png"  =>    "89504E47",
        "gif"  =>    "47494638",
        "tif"  =>    "49492A00",
        "bmp"  =>    "424D",
        "dwg"  =>    "41433130",
        "psd"  =>    "38425053",
        "rtf"  =>    "7B5C727466",
        "xml"  =>    "3C3F786D6C",
        "html  =>    "68746D6C3E",
        "eml"  =>    "44656C69766572792D646174",
        "dbx"  =>    "CFAD12FEC5FD746F",
        "pst"  =>    "2142444E",
        "xls"  =>    "D0CF11E0",
        "doc"  =>    "D0CF11E0",
        "mdb"  =>    "5374616E64617264204A",
        "wpd"  =>    "FF575043",
        "eps"  =>    "252150532D41646F6265",
        "ps"   =>    "252150532D41646F6265",
        "pdf"  =>    "255044462D312E",
        "pwl"  =>    "E3828596",
        "zip"  =>    "504B0304",
        "rar"  =>    "52617221",
        "wav"  =>    "57415645",
        "avi"  =>    "41564920",
        "ram"  =>    "2E7261FD",
        "rm"   =>    "2E524D46",
        "mpg"  =>    "000001BA",
        "mpg"  =>    "000001B3",
        "mov"  =>    "6D6F6F76",
        "asf"  =>    "3026B2758E66CF11",
        "mid"  =>    "4D546864",
        )
}


/**
 * 获取文件类型(通过读取文件前两个字节判断文件类型)
 * @param string $path 文件绝对路径
 * @return string 文件扩展名
 */
function get_file_type( $path = '' ) {
	$res = '';
	if ( file_exists($path) && is_readable($path) ) {
		$fh = fopen($path, 'rb');
		$bin = fread($fh, 2); //不一定只读前两个字节, 各个不同文件类型,头信息不一样。
		fclose($fh);

		$str_info  = unpack('C2chars', $bin); //"C2chars"中的“C”表示将给定二进制字符串解包为无符号字节型
		$type_code = intval($str_info['chars1'] . $str_info['chars2']);
		switch ( $type_code ) {
			case 3533:
				$res = 'amr';
				break;
			case 6677:
				$res = 'bmp';
				break;
			case 7790:
				$res = 'exe';
				break;
				break;
			case 7173:
				$res = 'gif';
				break;
			case 255216:
				$res = 'jpg';
				break;
			case 7368:
				$res = 'mp3';
				break;
			case 6063:
				$res = 'php';
				break;
			case 13780:
				$res = 'png';
				break;
			case 8297:
				$res = 'rar';
				break;
			case 4950:
				$res = 'txt';
				break;
			case 8075:
				$res = 'zip';
				break;
			default:
				$res = 'unknown' . $type_code;
		}
	} else {
		$res = !file_exists($path) ? 'file not exists' : ( !is_readable($path) ? 'not a readable file' : '' );
	}
	return $res;
}

  

posted on 2022-04-08 15:41  伊索  阅读(474)  评论(0编辑  收藏  举报