Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi all,
My requirement is something different.. i have done file uploading with validating by its extension in my module, but my client asked me don't allow any file to upload just by extension. bcoz it may possible that any .exe/.apk or other files can be renamed to extension .jpg/.png and uploaded..Check the contents of file to be uploaded..Check the contents of file to be uploaded.

Googling for the same and looking for help.. Thanks in advance.
Posted
Updated 10-Dec-15 19:25pm
v2
Comments
Sinisa Hajnal 11-Dec-15 2:48am    
You might have to check at file header, there is usually some bytes at the start ("header") that contain type and other metadata...but I honestly don't know which would be PDF sequence...or any other (well, except for BOF, but that hardly counts).

 
Share this answer
 
What you're looking for is called "magic numbers". File types have a sequence of binary data at the start of them that lets you know what the file type contains regardless of its extension. What you're going to have to do is find the magic numbers for the types you're interested in (gif, jpg, png, maybe pdf if you allow those) and check the files you have uploaded start with those numbers. If they don't then delete\reject them.

http://www.garykessler.net/library/file_sigs.html[^]

Some code to check for images, but you can adapt for any magic numbers;

http://stackoverflow.com/questions/670546/determine-if-file-is-an-image[^]

http://stackoverflow.com/questions/772388/c-sharp-how-can-i-test-a-file-is-a-jpeg[^]

If you google for things like "c# check image magic number" you'll find other examples.
 
Share this answer
 
v2
C#
if (fileupload.HasFile)
 {
     if (extension == ".exe"  || extension == ".apk" )
     {
     }
     else
     {
         // alert select only .exe or .apk files
     }
}
 
Share this answer
 
Here is the another most useful info i found.. thanks all

http://www.dotnetexpertguide.com/2011/05/validate-uploaded-image-content-in.html[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900