I usually use a module such as File::MMagic.
In reply to Re: Understanding File Type by chromatic in thread Understanding File Type by sanPerl