Page 1 of 1

How to get mime type of a file?

Posted: Mon Mar 29, 2010 12:54 pm
by flying_circus
My client uses a webhost running php 5.2.6, so fileinfo functions are not available. Mime-magic extension is not loaded, and use of dl() is denied.

At first, this project was going to deal with images, so I thought I might get away with using exif_imagetype() or getimagesize(). Now it has expanded to allowing file uploads of other formats as well.

Can you point me in the right direction?

Re: How to get mime type of a file?

Posted: Mon Mar 29, 2010 1:03 pm
by requinix
Looks like you'll have to roll your own.

The idea is to look at the first few bytes of a file and look them up in a table of sorts. It works for many types of files (images, PDF files, MS Office documents...) but there are some types where it doesn't always work.
FILExt.com is a good place to look.

Re: How to get mime type of a file?

Posted: Mon Mar 29, 2010 1:13 pm
by AbraCadaver
Well then your client has hamstrung you as the available tools aren't sufficient to meet the requirements. The best you can do here is exec() the linux 'file' command or if that's not available then build a solution that uses the file extension (not reliable).

Re: How to get mime type of a file?

Posted: Mon Mar 29, 2010 2:12 pm
by omniuni

Re: How to get mime type of a file?

Posted: Mon Mar 29, 2010 2:42 pm
by AbraCadaver
5.2.6 < 5.3.0 :-)

Re: How to get mime type of a file?

Posted: Mon Mar 29, 2010 3:01 pm
by flying_circus
AbraCadaver wrote:Well then your client has hamstrung you as the available tools aren't sufficient to meet the requirements. The best you can do here is exec() the linux 'file' command or if that's not available then build a solution that uses the file extension (not reliable).
While googling, I did find a little snippet that looked interesting. However, not being too familiar with *nix, was reluctant to actually use it.

If you have any thoughts, I'd like to get your opinion, otherwise I will spend some time reading later this week.

Code: Select all

$file = escapeshellarg($filename);
$mime = shell_exec("file -bi " . $file); 

Re: How to get mime type of a file?

Posted: Mon Mar 29, 2010 3:07 pm
by AbraCadaver
flying_circus wrote:
AbraCadaver wrote:Well then your client has hamstrung you as the available tools aren't sufficient to meet the requirements. The best you can do here is exec() the linux 'file' command or if that's not available then build a solution that uses the file extension (not reliable).
While googling, I did find a little snippet that looked interesting. However, not being too familiar with *nix, was reluctant to actually use it.

If you have any thoughts, I'd like to get your opinion, otherwise I will spend some time reading later this week.

Code: Select all

$file = escapeshellarg($filename);
$mime = shell_exec("file -bi " . $file); 
Yes, that's exactly right, and as far as I know is the same mechanism as the PHP extensions use. There may also be some "better" or more concise magic files such as the ones that come with Apache. 'file' has always worked for me and seems to get most common file types. I know that it doesn't get Gimp .xcf.

You can also use function_exists() or is_callable() to test for the preferred functions and if not found use 'file'.

Re: How to get mime type of a file?

Posted: Mon Mar 29, 2010 5:47 pm
by omniuni
AbraCadaver wrote:
5.2.6 < 5.3.0 :-)
Oh, wow. I could have sworn I used that in a PHP4 script once... sorry!