Home > Backend Development > PHP Tutorial > PHP method to determine file type based on binary (file header) information

PHP method to determine file type based on binary (file header) information

WBOY
Release: 2016-07-25 09:00:52
Original
1081 people have browsed it
  1. $files = array('./test.jpg', 'test.png');
  2. $fileTypes = array(
  3. 7790 => 'exe',
  4. 7784 => 'midi',
  5. 8075 => 'zip',
  6. 8297 => 'rar',
  7. 225216 => 'jpg',
  8. 7173 => 'gif',
  9. 6677 => 'bmp',
  10. 13780 => 'png',
  11. );
  12. foreach($files as $file) {
  13. $fp = fopen($file, 'rb');
  14. $bin = fread($fp, 2); // 只读头两个字节
  15. fclose($fp);
  16. $strInfo = @unpack("C2chars", $bin);
  17. $typeCode = intval($strInfo['chars1'].$strInfo['chars2']);
  18. $fileType = isset($fileTypes[$typeCode]) ? $fileTypes[$typeCode] : 'unknown';
  19. echo $file , ' type : ', $fileType, ' code : ', $fileType, '
    ';
  20. }
复制代码


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 Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template