tags:

views:

41

answers:

3

hello

how to identify the exact file type of a file for better understanding i am giving some more

details:

for example if i have a file named as "example.exe" then i can easily recognize that it's an

windows executable file(by seeing the extension .exe)but if i remove the extension(.exe)then

by seeing i am not able to identify the type of the file.

then now how can i identify the file type?

(Please suggest your answers using c/c++,java,python,php(For web uploads))

-Thanks

Peeyush Chandel

(INDIA)

+2  A: 

There's no such thing as "exact file type". Binary data is binary data.

If you're running on a POSIX-like system, you can use the file command to guess the file type. I don't think this gives you a MIME type.

If your server is running Apache, then you can use mod_mime_magic to make a guess.

If you're using PHP, you can install the fileinfo extension.

tc.
Have a look at “file --mime-type” if you need the MIME type.
Scytale
+1  A: 

You need to know the specification of each file type you want to handle.

With this specification you can create a method to check if a given file is of a specific type.

Example:

isExe(File)
isJpg(File)
Daniel Moura
A: 

If you want to find a file extention, try to use this trivial code:

$ext = pathinfo($filename, PATHINFO_EXTENSION);
Alexander.Plutov