Php reads two bytes from the file header to determine the actual file type

Source: Internet
Author: User
Tags dbx corel draw
FunctioncheckFileType ($ fileName) {$ filefopen ($ fileName, rb); $ binfread ($ file, 2); read-only 2 bytes fclose ($ file); $ strInfo @ unpack (C2chars, $ bin); C is an unsigned integer, and all found online are c, which is a signed integer, which produces negative numbers and leads to abnormal judgment. $ typeCodeint

Function checkFileType ($ fileName) {$ file = fopen ($ fileName, rb); $ bin = fread ($ file, 2); // read-only 2 bytes fclose ($ file ); $ strInfo = @ unpack (C2chars, $ bin); // C is an unsigned integer. All searched online are c, which is a signed integer, this will generate a negative number to judge the exception $ typeCode = int

Function checkFileType ($ fileName) {$ file = fopen ($ fileName, "rb"); $ bin = fread ($ file, 2 ); // read-only 2-byte fclose ($ file); $ strInfo = @ unpack ("C2chars", $ bin); // C is an unsigned integer and all searched online are c, it is a signed integer, which produces a negative number. it is not normal to judge $ typeCode = intval ($ strInfo ['chars1']. $ strInfo ['chars2']); $ fileType = ''; if ($ typeCode = 255216/* jpg */| $ typeCode = 7173/* gif */| $ typeCode = 13780/* png */) {return true;} else {return false ;}}

File Header. Check the File Header to determine the File type.

You can check the binary file to find the matching format of the file even if the file suffix is changed. Of course, this is the method used by the player to identify the file.
1. Extracted from Ultra-edit-32

JPEG (jpg), file header: FFD8FF
PNG (png), file header: 89504E47
GIF, file header: 47494638
TIFF (tif), file header: 492.16a00
Windows Bitmap (bmp), file header: mongod
CAD (dwg), file header: 41433130
Adobe Photoshop (psd), file header: 38425053
Rich Text Format (rtf), file header: 7B5C727466
XML (xml), file header: 3C3F786D6C
HTML (html), file header: 68746D6C3E
Email [thorough only] (eml), file header: 44656c69766572792d646174243a
Outlook Express (dbx), file header: CFAD12FEC5FD746F
Outlook (pst), file header: 2142444E
MS Word/Excel (xls.or.doc), file header: D0CF11E0
MS Access (mdb), file header: 5374616e64617213204a
WordPerfect (wpd), file header: FF575043
Postscript (eps. or. ps), file header: 252150532D41646F6265
Adobe Acrobat (pdf), file header: 255044462D312E
Quicken (qdf), file header: AC9EBD8F
Windows Password (pwl), file header: E3828596
ZIP Archive (zip), file header: 504B0304
RAR Archive (rar), file header: 52617221
Wave (wav), file header: 57415645
AVI (avi), file header: 41564920
Real Audio (ram), file header: 2e00001fd
Real Media (rm), file header: 2E524D46
MPEG (mpg), file header: 000001BA
MPEG (mpg), file header: 000001B3
Quicktime (mov), file header: 6D6F6F76
Windows Media (asf), file header: 3026B2758E66CF11
MIDI (mid), file header: 4D546864

Ii. List of file headers retrieved from winhex
File Type ExtensionsHeader
JPEG jpg; jpeg 0xFFD8FF
PNG png 0x89504E470D0A1A0A
GIF gif GIF 8
TIFF tif; tiff 0x4920.a00
TIFF tif; tiff 0x4D4D002A
Bit map bmp BM
Aol art art 0x4A47040E000000
Aol art art 0x4A47030E000000
PC Paintbrush pcx 0x0A050108
Graphics Metafile wmf 0xd7csc69a
Graphics Metafile wmf 0x01000900
Graphics Metafile wmf 0x02000900
Enhanced Metafile emf 0x0100000058000000
Corel Draw cdr
CAD dwg 0x41433130
Adobe Photoshop psd 8BPS
Rich Text Format rtf
XML xml
HTML html; htm; php; php3; php4; phtml; shtml type
Email eml Delivery-date:
Outlook Express dbx 0xCFAD12FE
Outlookpst! BDN
MS Office/OLE2 doc; xls; dot; ppt; xla; ppa; pps; pot; msi; sdw; db 0xD0CF11E0A1B11AE1
MS Access mdb; mda; mde; mdt Standard J
WordPerfect wpd 0xFF575043
OpenOffice Writer sxw writer
OpenOffice Calc sxc calc
OpenOffice Math sxm math
OpenOffice Impress sxi impress
OpenOffice Draw sxd draw
Adobe FrameMaker fm PostScript eps. or. ps; eps %! PS-Adobe
Adobe Acrobat pdf % PDF-1.
Quicken qdf 0xAC9EBD8F
QuickBooks Backup qbb 0x458600000600
Sage sly. or. srt. or. slt; sly; srt; slt 0x53520100
Sage Backup 1 SAGEBACKUP
Lotus WordPro v9 lwp 0x576f000000000000f
Lotus 123 v9 123 0x00001A00051004
Lotus 123 v5 wk4 0x00001A0002100400
Lotus 123 v3 wk3 0x00001A0000100400
Lotus 123 v1 wk1 0x2000604060
Windows Password pwl 0xE3828596
ZIP Archive zip; jar 0x504B0304
ZIP Archive (outdated) zip 0x504B3030
RAR Archive rar Rar!
GZ Archive gz; tgz 0x1F8B08
BZIP Archive bz2 BZh
ARJ Archive arj 0x60EA
7-ZIP Archive 7z 7z set'
Wave wav WAVE
AVI avi
Real Audio ram; ra. ra? 0
Real Media rm. RMF
MPEG mpg; mpeg 0x000001BA
MPEG mpg; mpeg 0x000001B3
Quicktime mov moov
Windows Media asf 0x3026B2758E66CF11
MIDI mid MThd
Win32 Executable exe; dll; drv; vxd; sys; ocx; vbx MZ
Win16 Executable exe; dll; drv; vxd; sys; ocx; vbx MZ
ELF Executable elf; 0x7F454C4601010100

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.