本文实例讲述了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程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!