Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting extension of the file in FileUpload Control

At the moment i get file extension of the file like :

string fileExt = System.IO.Path.GetExtension(filUpload.FileName);

But if the user change the file extension of the file ( for example user could rename "test.txt" to "test.jpg" ), I can't get the real extension . What's the solution ?

like image 746
Mostafa Avatar asked Mar 14 '10 16:03

Mostafa


1 Answers

You seem to be asking if you can identify file-type from its content.

Most solutions will indeed attempt the file extension, but there are too many different possible file types to be reliably identifiable.

Most approaches use the first several bytes of the file to determine what they are.

Here is one list, here another.

If you are only worried about text vs binary, see this SO question and answers.

See this SO answer for checking if a file is a JPG - this approach can be extended to use other file headers as in the first two links in this answer.

like image 186
Oded Avatar answered Sep 20 '22 16:09

Oded