Re: How to determine MIME type of a file
| From: | Matthew Brealey | Date: | Wed, 18 Oct 2000 10:52:25 +0000 |
| Subject: | Re: How to determine MIME type of a file | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-20865@lists.php.net to get a copy of this message | ||
You wrote:
>
> Hi
>
> In a PHP application, I need to be able to extract the MIME type of files on
> the server (they're getting uploaded to the server via internal network). I
> could not find how to do this in Apache/PHP. To use the file extension has
> proven not to be reliable. I tried imap_mime_header_decode, but it expects a
> mail message as the string.
Unless, for some strange reason, you are hosting on Windows, call the
file command (e.g.,
switch (exec("file $upload_filename")) {
case ".....":
store($upload_filename);
break;
case "....":
store($upload_filename);
break;
default:
die("Sorry, that's not a valid file. Please upload only xxxx files");
break;
}
You'll have to check what file returns for each of your required
filetypes (or just read the source). [Typically something like file
rich.gif returns:
rich.gif: GIF image data, version 87a, 513 * 72. Since for some
filetypes the returned string differs (e.g., with image dimensions),
you might (depending on what files you want) have to check inside the
returned string (e.g., for presence of 'GIF'.).
This should return the type of the file reasonably accurately. [It's
certainly a lot better than extension detection - an agency I
registered with only accepted CVs in MS Word format. Simply changing my
.html to .doc fooled them.
-- Random fortune
The English instinctively admire any man who has no talent and is modest
about it.
-- James Agate, British film and drama critic