John Smith
John Smith

Reputation: 394

Fileinfo: MIME-Type AND Description

I'm using fileinfo to get MIME-Types and description. At the moment I create two finfo Objects, like this:

$x = new finfo(FILEINFO_NONE, ....);
print $x->file($filename);
print '\n';
$x = new finfo(FILEINFO_MIME, ....);
print $x->file($filename);

This results in something like this:

application/pdf; charset=binary
PDF document, version 1.2

It works fine for me, but is there a way to get description and mime info in one call?

Upvotes: 2

Views: 3666

Answers (3)

za_al
za_al

Reputation: 21

If you want to revert to the old values, without charsets, PHP 5.3 introduced the FILEINFO_MIME_TYPE constant. Switching from FILEINFO_MIME to FILEINFO_MIME_TYPE

 if( class_exists('finfo')&&defined('FILEINFO_MIME'))
   {
       $finfo = new finfo(FILEINFO_MIME);
       return $finfo->file($_FILE['tempname']);

   }

for example for php_file return: text/x-php; charset=utf-8

but following code:

if( class_exists('finfo')&&defined('FILEINFO_MIME_TYPE'))
   {
       $finfo = new finfo(FILEINFO_MIME_TYPE);
       return $finfo->file($_FILE['tempname']);

   }

for php_file return: text/x-php

see following link: http://debugsober.com/blog/PHP-53-Fileinfo-and-weird-mime_type-charsets

Upvotes: 1

John Smith
John Smith

Reputation: 394

Uh, is really simple, i don't know how i could not have seen this earlier: For the method "file", there is a second optional argument, like this:

$x = new finfo(FILEINFO_NONE, ....);
print $x->file($filename);
print '\n';
print $x->file($filename, FILEINFO_MIME);

Upvotes: 2

Brad
Brad

Reputation: 163232

This is untested, but the documentation states that the first parameter of finfo_open() accepts "one or disjunction of more Fileinfo constants", which means that we should be able to OR a couple constants together:

$x = new finfo(FILEINFO_NONE | FILEINFO_MIME, ...);

Upvotes: 0

Related Questions