php文件类型检测:根据二进制文件头判断文件类型的函数
时间:2021-07-01 10:21:17
帮助过:17人阅读
- $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, '
'; - }
|