Home  >  Article  >  Backend Development  >  php通过文件头判断格式的方法,_PHP教程

php通过文件头判断格式的方法,_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:50:53658browse

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

本文实例讲述了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;
}
}

补充:小编在这里推荐一款本站的php格式化美化的排版工具帮助大家在以后的PHP程序设计中进行代码排版:

php代码在线格式化美化工具:

http://tools.jb51.net/code/phpformat

另外,由于php属于C语言风格,因此下面这款工具同样可以实现php代码的格式化:

C语言风格/HTML/CSS/json代码格式化美化工具:
http://tools.jb51.net/code/ccode_html_css_json

更多关于PHP相关内容感兴趣的读者可查看本站专题:《PHP数学运算技巧总结》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《PHP数组(Array)操作技巧大全》、《php排序算法总结》、《PHP常用遍历算法与技巧总结》、《PHP数据结构与算法教程》、《php程序设计算法总结》、《php正则表达式用法总结》、《PHP运算与运算符用法总结》、《php字符串(string)用法总结》及《php常见数据库操作技巧汇总》

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

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133060.htmlTechArticlephp通过文件头判断格式的方法, 本文实例讲述了php通过文件头判断格式的方法。分享给大家供大家参考,具体如下: function judgeFile($file,...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn