Reputation: 2425
My application is allowed users to upload jpeg/png files.I must to detect files with changed extensions.For example: if somebody take example.txt file and change his name to example.jpg so it not enough to check this file by javascript, need server solution. How i can do this? Can somebody show code example? Thanks
Upvotes: 1
Views: 325
Reputation: 11256
You can do this most of the time using magic bytes in the beginning of files. See this link for a list of these numbers for different file types: http://www.astro.keele.ac.uk/oldusers/rno/Computing/File_magic.html
For instance, JPEG files start with ff d8 ff e0
, and png files start with 89 50 4e 47
.
Upvotes: 5
Reputation: 62256
The file with not correct format, for your application (considering post) is an exceptional situation. So I would think about simple try/catch
block where you try to load image of declared format. If exception ocures (say) => notify to a user, that the file seems to be in non correct format.
Upvotes: 3