Home >Backend Development >PHP Tutorial >How to determine the format in php through the file header, _PHP tutorial

How to determine the format in php through the file header, _PHP tutorial

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

How PHP determines the format through the file header,

The example in this article describes how PHP determines the format through the file header. Share it with everyone for your reference, the details are as follows:

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;
}
}

Supplement: The editor here recommends a PHP formatting and beautifying typesetting tool on this website to help you code typesetting in future PHP programming:

php code online formatting and beautification tool:

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

In addition, since php belongs to the C language style, the following tool can also format php code:

C language style/HTML/CSS/json code formatting and beautification tool:
http://tools.jb51.net/code/ccode_html_css_json

Readers who are interested in more PHP-related content can check out the special topics on this site: "Summary of PHP mathematical operation skills", "Summary of PHP operating office document skills (including word, excel, access, ppt)", "PHP array ( Array) operating skills collection", "php sorting algorithm summary", "php common traversal algorithms and techniques summary", "php data structure and algorithm tutorial", "php programming algorithm summary", "php regular expression usage summary", "Summary of PHP operations and operator usage", "Summary of PHP string usage" and "Summary of common PHP database operation skills"

I hope this article will be helpful to everyone in PHP programming.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1133060.htmlTechArticleHow PHP determines the format through the file header. This example describes how PHP determines the format through the file header. Share it with everyone for your reference, the details are as follows: 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