Home > Backend Development > PHP Tutorial > PHP file type detection: function to determine file type based on binary file header

PHP file type detection: function to determine file type based on binary file header

WBOY
Release: 2016-07-25 08:51:34
Original
909 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