|
本文实例讲述了php通过文件头判断格式的方法。分享给大家供大家参考,具体如下:
function judgeFile($file,$form){
if(!empty($file) && !empty($form)){
$filehead = fopen($file,'r');
$bin = fread($filehead, 2);
fclose($filehead);
$data = unpack('C2chars', $bin);
$type_code = intval($data['chars1'].$data['chars2']);
switch ($type_code) {
case 7790: $fileType = 'exe';break;
case 7784: $fileType = 'midi';break;
case 8075: $fileType = 'zip';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: $fileType = 'unknown';break;
}
if(!is_array($form)){
if($fileType==$form){
return true;
}else{
return false;
}
}else{
if(in_array($file, $form)){
return true;
}else{
return FALSE;
}
}
}else{
return false;
}
}
|
|