Fileinfo inconsistent, returning wrong MIME type

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View
if (!function_exists('mime_content_type_fileinfo')) {
  * Will use {@link FileInfo} functions
provided within {@link PECL} bundle to return mime
  * @param string $file
  * @return string $mime_type
  * @see mime_content_type
 function &mime_content_type_fileinfo($file) {
   global $windowsMagicMimePath, $mimeTypeFilePath;
   $mimePath = (($_ENV['windir'] || $_SERVER['windir']) &&
$windowsMagicMimePath) ? $windowsMagicMimePath : $mimeTypeFilePath;
   if ($_ENV['windir'] || $_SERVER['windir']) $mimePath =
preg_replace('/\.mime$/i', '', $mimePath);
   if (class_exists('FileInfo')) {
    $finfo =& new FileInfo(FILEINFO_MIME, $mimePath);
    if (is_object($finfo)) {
     return $finfo->file($file);
    } else if (function_exists('finfo_open')) {
     $mime = finfo_file(finfo_open(FILEINFO_MIME, $mimePath), $file);
     if ($mime) return mime; else return mime_content_type($file);
    } else {
     return mime_content_type($file);
   } else if (function_exists('finfo_open')) {
    $mime = finfo_file(finfo_open(FILEINFO_MIME, $mimePath), $file);
    if ($mime) return $mime; else return mime_content_type($file);
   } else {
    return mime_content_type($file);

WAMP 5.0, WinXP, PHP 5.2.0, Apache 2.0

Whenever I run this function within SAPI PHP, though I have added this
to php.ini:


And restarted web services, class_exists('FileInfo') is always false,
furthermore, function_exists('finfo_open') is also always false

That I absolutely do not get

However, in CLI PHP, class_exists('FileInfo') is still false, however
function_exists('finfo_open') comes back true this time

But, if I try to use this line on a JPEG image:

echo finfo_file(finfo_open(FILEINFO_MIME, 'C:/wamp/php/extras/mime'),

I wind up instead of 'image/jpeg', this instead:


Anyone who knows how to use FileInfo is welcome to play around with
function to make it fit, but it defaults to mime_content_type()
even though it's deprecated.


Re: Fileinfo inconsistent, returning wrong MIME type

Quoted text here. Click to load it


Ready for this one?

I did what was suggested: putting in "path" as both user and system
environmental variable, rebooted my machine.. NO PECL

Then I added the path to the PHP executable to both user and system
environmental variables, rebooted my machine.. STILL NO PECL

Then I finally decided to just "wing it". I tried everything my
imagination could deliver to me..

When I got to the part where I needed to add lines to wampserver.ini
recognize php_fileinfo.dll with the "on" option, I got PECL!

In fact, I found that through backward engineering the problem, that
was only one solution:

1) Have php_fileinfo.dll within PHP's php.ini
2) Have php_fileinfo.dll within Window's php.ini
3) Have php_fileinfo.dll within wampserver.ini

If any of these steps are not included, alongside the obvious web
restart, then PECL is not recognized.

In fact, I was able to get full PECL recognition even after I took out
"path" user defined environmental variable AND even after removing
paths to the PHP executable from the "path" system defined

Thanx all!

Site Timeline