To validate file upload types used Path.GetExtension(Filename) to return the file extension. But if a user change file extension to different one and try to upload, how can I read the actual extension of the file
Asked
Active
Viewed 103 times
0
-
2You can't. The extension is just a description, a reference used to link a file type to the program that should open it and/or for organization purposes. It doesn't determine the file content. If you are expecting some specific file types, you could inspect the file header and see if it matches the description. – Jimi Dec 13 '18 at 11:12
-
1You can think of a file extension like a label on a storage jar. If you have a jar with a label saying "Sugar" on it then you would generally expect to find sugar in it but you can't be sure of that. The only way to know for sure is to look inside and test the contents somehow to see whether it is actually sugar. File are the same. The extension is supposed to tell you what's in the file but it's an implicit relationship. To know for sure, you have to look at the file contents yourself. – jmcilhinney Dec 13 '18 at 11:24
-
1By the way, the extension is the extension. There's no different "actual extension". What you're talking about is the file type. For instance, a JPEG file will normally have an extension of ".jpg" or ".jpeg". ".bin" and ".dat" are common extensions for binary and data files but the actual format could be one of hundreds or even thousands that commonly use those extensions. The type and the extension are related but they are not the same thing. – jmcilhinney Dec 13 '18 at 11:24
1 Answers
2
There exists Mime Detective which can get the mime type of a file by looking at its headers.

viking_grll
- 131
- 8