How to get mime type of a file?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

How to get mime type of a file?

Post 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?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: How to get mime type of a file?

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to get mime type of a file?

Post 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).
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: How to get mime type of a file?

Post by omniuni »

User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to get mime type of a file?

Post by AbraCadaver »

5.2.6 < 5.3.0 :-)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: How to get mime type of a file?

Post 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); 
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: How to get mime type of a file?

Post 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'.
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
omniuni
Forum Regular
Posts: 738
Joined: Tue Jul 15, 2008 10:50 pm
Location: Carolina, USA

Re: How to get mime type of a file?

Post 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!
Post Reply