MIME file type identifier
Determine file MIME type or extension based on its signature or magic bytes.
MIME File Type Idenfitier
MIME is media type often abbreviated from Multipurpose Internet Mail Extensions.
How we detect MIME Type from a file? Well, we read the file signatures, also known as 'magic bytes'.
More advance explanation
So, we can ready the file signatures, mostly from the header / first few bytes of file. Then, we can match the bytes to coresponding unique magic bytes
Example
For example, we have a file with following bytes (we cut few bytes for the sake of simplicity)
89 50 4E 47 0D 0A 1A 0A
BA BE FA CE AA BB CC DD
CA FE D0 D0 BE EE EE EF
These signatures 89 50 4E 47 0D 0A 1A 0A
match PNG signature, hence it will be treated as PNG file (this condition true on most linux systems)
Cool facts:
- if you use linux systems you will notice that the linux will treat the file based on the file signatures, not extension