Home > php教程 > php手册 > body text

PHP二进制判断文件类型

WBOY
Release: 2016-06-06 19:47:08
Original
1149 people have browsed it

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别。 ?php $files = array('./test.jpg', 'test.png'); $fileTypes = a

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

  一般我们都是按照文件扩展名来判断文件类型,但是这个很不靠谱,轻易就通过修改扩展名来躲避了,一般必须要读取文件信息来识别。

  

  $files = array('./test.jpg', 'test.png');

  $fileTypes = array(

  7790    => 'exe',

  7784    => 'midi',

  8075    => 'zip',

  8297    => 'rar',

  225216  => 'jpg',

  7173    => 'gif',

  6677    => 'bmp',

  13780   => 'png',

  );

  foreach($files as $file) {

  $fp = fopen($file, 'rb');

  $bin = fread($fp, 2); // 只读头两个字节

  fclose($fp);

  $strInfo = @unpack("C2chars", $bin);

  $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);

  $fileType = isset($fileTypes[$typeCode]) ? $fileTypes[$typeCode] : 'unknown';

  echo $file , ' type : ', $fileType, ' code : ', $fileType, '
';

  }

PHP二进制判断文件类型

Related labels:
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!