On Tue, Jun 19, 2001 at 03:41:40PM -0500, Jeff Wagg wrote:
> 
> Hi,
>       I was just wondering if there were any file test operators (or
> modules containing these operators) which tested files for their type. For
> example, is there a way to test a file to see whether or not it's in FITS
> format or whether or not the file is a GIF file? Any help would be great.
> 

If you're on a Unix(-ish) system, have a look at 'magic'. man magic will
probably help some, or you could go straight to the magic file itself
(on my system, it's /usr/share/magic/magic). Or you could just use the
'file' command:
$filetype = `file $filename`;

For simple cases, it's not too hard to do by hand. Gif files, for
example, always start with "GIF89a" or a similar string depending on the
version of the GIF standard.

Note, though, that magic's heuristics aren't bulletproof. They're still
probably better than what you could come up with by hand.

<insert standard rant about windows being brain-dead in its reliance on
and trust of file extensions>.

A quick search for 'magic' on CPAN throws up File::MMagic, but I have no
idea how good it is.

cheers
rob c

Reply via email to