当前位置:Gxlcms > PHP教程 > PHP取二进制文件头判断文件类型

PHP取二进制文件头判断文件类型

时间:2021-07-01 10:21:17 帮助过:14人阅读

  1. /**
  2. * PHP取二进制文件头快速判断文件类型
  3. *
  4. * @param 文件 $file
  5. * @return string
  6. * @author lrenwang
  7. * @blog blog.lrenwang.com
  8. */
  9. function get_extname($file)
  10. {
  11. $fp = fopen($file, "rb");
  12. $bin = fread($fp, 2); //只读2字节
  13. fclose($fp);
  14. $bin_info = @unpack("C2chars", $bin);
  15. $code = intval($bin_info['chars1'].$bin_info['chars2']);
  16. switch ($code) {
  17. case 7790:
  18. $type = 'exe';
  19. break;
  20. case 7784:
  21. $type = 'midi';
  22. break;
  23. case 8075:
  24. $type = 'zip';
  25. break;
  26. case 8297:
  27. $type = 'rar';
  28. break;
  29. case 255216:
  30. $type = 'jpg';
  31. break;
  32. case 7173:
  33. $type = 'gif';
  34. break;
  35. case 6677:
  36. $type = 'bmp';
  37. break;
  38. case 13780:
  39. $type = 'png';
  40. break;
  41. default:
  42. $type = $code;
  43. break;
  44. }
  45. return $type;
  46. }
  47. ?>

人气教程排行