服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - PHP教程 - php通过文件头判断格式的方法

php通过文件头判断格式的方法

2021-01-24 15:45鹰眼 PHP教程

这篇文章主要介绍了php通过文件头判断格式的方法,涉及php文件读取及字符串解析的相关技巧,需要的朋友可以参考下

本文实例讲述了php通过文件头判断格式的方法。分享给大家供大家参考,具体如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
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;
}
}

希望本文所述对大家PHP程序设计有所帮助。

延伸 · 阅读

精彩推荐