0

Basically this question except i am using ASP.NET. How do i take a given file and check if it is

  • image (all formats except bitmap including photoshop)
  • archive (7z, rar, zip, etc). NOTE: I know a file can be both an image and zip. I can check if its both in code.
  • sound (most such as m4a, flac, ogg, but not wav)
  • model (3ds, X, B3D, etc)

The solution does not need to check all of these but if i can get a few to check all of these then great.

Community
  • 1
  • 1

2 Answers2

2

You can try to either:

  • Find documentation for the file format and check for known bytes in the file header, etc
  • Attempt to load the file using an API - if the file loads successfully then you know it is of the proper type.

For example, here is an example of how to verify an image file: validate-image-from-file-in-c#

Of course, the mime type and/or file extension can give you a good guess of what file format you are dealing with, but (depending on the source) you should not rely on it exclusively.

Community
  • 1
  • 1
Justin Ethier
  • 122,367
  • 49
  • 219
  • 273
1

Given the variety of your file types, checking the extension may be the best option. Anyone has other thoughts?

Winston Chen
  • 6,393
  • 11
  • 47
  • 78
  • This is a good first step, but you really should do a more thorough examination, especially if files will be uploaded by anyone on the Internet... – Justin Ethier Mar 11 '10 at 03:21
  • yes, I totally agree. The uploaded file may be virus or some dry-by-downloads. What I did was that I always rename the file with my desired extensions in order to prevent auto-run. But this is nothing close to detecting the file type though. – Winston Chen Mar 11 '10 at 03:25