I want to validate a type of an uploaded file (suppose the file has incorrect extension).
I figure it could be done by checking the magic number of file signature. Similarly, as it is done in unix. So I guess I need to read the first bytes of the file and compare them with the magic number mappings (and I need to have the mappings and I need to maintain them).
So I thought maybe there is a class out there just for this purpose? Or maybe someone has done this and can share their insights on the solution? Thanks
I doubt there's a “silver bullet” solution for this problem. Nobody says every file format has any form of a magic number. Consider plain text files — no reasonable indication at all (beside a possible byte-order-mark at the beginning which, however, need not mean anything) and pure heuristics must be used to guess it's a plain text file. Like wise to recognize an XML or HTML (i.e. SGML) file it means to check a lot of possibilities.
The approach you should follow is:
In case the set is narrow “enough” I'd consider a simple custom solution as the right one.