I'm installing this module on two instances of Drupal on the same server, one is 5.x and one is 6.x. I get the following message on both:

"Fileinfo could not load the magic file. It could be corrupted. Try reinstalling the magic file distributed with the MimeDetect module. (Currently using Mime type detection PHP Fileinfo Extension)"

I've tried with various file permissions, but beyond that I don't know. It's likely to be a server config issue.

From status report:

Apache/2.2.9 (Unix) mod_ssl/2.2.9 OpenSSL/0.9.7a mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635 PHP/5.2.6

Thanks for the help.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

rj’s picture

I am having this problem too, any help would be appreciated. This is on Drupal 5.10; I have the same web server results from the status report.

matt_paz’s picture

I have the same experience on D6 using Windows/IIS

Zach Harkey’s picture

Reporting same problem here. Been troubleshooting off and on for days.

seaneffel’s picture

Title: Can't install magic file » Fileinfo could not load the magic file

Changed name to better reflect the issue we're having. That magic file isn't so magical, now is it.

nnn’s picture

I guess the magic file supplied by the module have some sort of conflict with the fileinfo extension installed on the server. In my server that complain about this, I found the system installed magic files in /usr/share. So I made the follow change - change the line below in the function mimedetect_requirements
if (!$finfo = @finfo_open(FILEINFO_MIME, drupal_get_path('module', 'mimedetect') .'/magic')) {
to
if (!$finfo = @finfo_open(FILEINFO_MIME, '/usr/share/magic')) {

after that, I saw no more of this error.

If you do so, then you should also change the similar line in function mimedetect_mime, I think.

Maybe the author of this module could add another config variable, and have the website admin fill in where they have their magic files, if they have fileinfo enabled at all.

Zach Harkey’s picture

Tried everything for days, nnn's suggestion in comment #5 finally got rid of the error without affecting (as far as I can tell) the behavior of the module.

tamasd’s picture

Version: 6.x-1.0-beta1 » 5.x-1.0
Status: Active » Needs review
FileSize
723 bytes

I wrote an oneliner patch to fix this issue.

drewish’s picture

Version: 5.x-1.0 » 6.x-1.x-dev
FileSize
1.08 KB

here's the patch for D6.

drewish’s picture

Status: Needs review » Needs work

actually this needs to update mimedetect_mime() since that's also trying to load the "magic" file... i need to get the fileinfo extension built so i can test if we should be passing it "magic.mime" or "magic.mgc".

drewish’s picture

humm... actually this might be a won't fix since http://us3.php.net/manual/en/function.finfo-open.php says:

magic_file

Name of a magic database file, usually something like /path/to/magic.mime. If not specified, the MAGIC environment variable is used. If this variable is not set either, /usr/share/misc/magic is used by default. A .mime and/or .mgc suffix is added if needed.

drewish’s picture

I'm now starting to think that it would be best to have a setting that specifies the path to the magic database. We could use the one that ships with mimedetect as the default value. Thoughts?

seaneffel’s picture

Drewish, I think this is a good idea. Let the users decide which file to use, leave the default set to the easiest choice, have good help text that explains to users why they would change that default value.

drewish’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Needs work » Needs review
FileSize
718 bytes

this just adds a variable for the magic file with no form for editing it.

brianbrown’s picture

I have this issue also. Manually uninstalled (my Drupal module uninstaller doesn't work), including database entries, reinstalled with patched and module installer won't even allow it to be activated.
Thanks!
-Brian

GuillaumeDuveau’s picture

Status: Needs review » Reviewed & tested by the community

Patch in #13 is fine, but maybe a little documentation would be nice.

brianbrown’s picture

Installed on a "clean and sober" new install. Patch makes no difference. Uninstalling module.

GuillaumeDuveau’s picture

Status: Reviewed & tested by the community » Needs work

Do you have libmagic installed on your server ? If no try to install it and use the variable to set the path ?

GuillaumeDuveau’s picture

Category: support » bug
Status: Needs work » Needs review
FileSize
2.82 KB

Changing this to a bug report, because the magic database provided by Mimedetect does not always work. You have to get your server distribution version. This patch updates the README.txt with these explanations, adds Drewish fix and the same in mimedetect.install.

Advice : first try with basic PHP, outside Drupal, if you can load the database :

<?php
$magic_file = '/usr/share/file/magic';
$finfo = finfo_open(FILEINFO_MIME, $magic_file);
if (!$finfo) {
    echo "Opening fileinfo database failed";
    exit();
}
?>

Replace '/usr/share/file/magic' by the correct path if it's different.

seaneffel’s picture

In the docs of this module, will it describe to your average user how to identify the correct path to the magic file if it turns out that the default doesn't work upon install? Might reduce the issues we all have to answer down the road. Just a thought.

GuillaumeDuveau’s picture

We can not say more that it comes with File 4.x. Installing a PHP extension is not an average user task, too, I guess ?

GuillaumeDuveau’s picture

Anyway I just added the simple PHP test in the README.txt.

mjhammel’s picture

I tried the patch from guix in #21. It has a minor bug - there is an extra space in the source file line to be removed that prevented the patch from working. Instead of

$magic_file = drupal_get_path('module', 'mimedetect') . '/magic';

(extra space between the period and quote right before /magic) it should be

$magic_file = drupal_get_path('module', 'mimedetect') .'/magic';

At least that's how I got it to apply.

bcobin’s picture

Version: 7.x-1.x-dev » 6.x-1.x-dev

Terrific work - patch applied with the mad as listed in #22 - thanks much!

drewish’s picture

FileSize
3.09 KB

The new docs are a great addition. I've done a little clean up on the to wrap long lines and get the punctuation to match the rest of the file.

drewish’s picture

Status: Needs review » Fixed

committed to HEAD and DRUPAL-6--1

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

Anonymous’s picture

Status: Closed (fixed) » Active

The documentation mentions to add a setting in settings.php like 'mimedetect_magic' => '/usr/share/file/magic'

In my case, I had to change it to:

'mimedetect_magic' => '/usr/share/file/magic.mime'

Notice the .mime extension! This apparently needed on my linux debian VPS.

Also, I had to overcome an open_basedire restriction by editing the vhost.conf to add /usr/share/fil.

rmiddle’s picture

subscribe

jacobson’s picture

Just for clarification. The documentation in the patch file says to add the following line to the settings.php file:

'mimedetect_magic' => 'usr/share/file/magic.mime'

On my system, a leading slash was required before "usr" to make the module work. I too had to add .mime to the file name.

annajonna’s picture

Did some research on the two files on my server. Btw an Ubuntu (8.04) Long Term Support release.
The result is that those two files are very different in size.

480K /usr/share/file/magic 
 32K  /usr/share/file/magic.mime 

Both files are text files. I would suspect that /usr/share/file/magic would support many more filetypes than the smaller file /usr/share/file/magic.mime .
There are also binary versions of those files and they are even bigger.

Th30philus’s picture

Running on Drupal 6.16, I applied this patch to MimeDetect 1.2 (and 1.x-dev) (and specified the mimedetect_magic variable in settings.php (both with and without the .mime extension)), but I still get the error. The simple test from comment 18 works, but MimeDetect can't load the magic file. Any thoughts on a fix?

Th30philus’s picture

Component: Code » Documentation
Status: Active » Needs work

Solved. I copied and pasted the variable assignment from the README, but as pointed out in comment 29, the README lacks the leading "/". This fixed it for me. I did not need to add the .mime, BTW.

seaneffel’s picture

I added the following lines to my settings.php as described in the readme.txt file in the module directory. I too had to add a slash in front of the "usr/..." path in order for my server to detect this magic file, as seen below.

$conf = array(
    'mimedetect_magic' => '/usr/share/file/magic',
  );

The key to good docs here is referencing some link to what this PHP extension is, and noting for new Drupal site admins that means a conversation with their web host if it is not working. I agree with the comment in #20 above, but suggesting a place to start for troubleshooting is a nice effort for users.

Here are the verbose changes to the docs I suggest, I'm working on a patch file - though I don't have a lot of skill in creating those yet. BRB.


Background
===============
MimeDetect strives to provide consistent and accurate server side MIME type detection. It supports the PHP FileInfo Extension, the UNIX 'file' command, then tries to use the MIME type supplied with the file object. If everything fails it will select a MIME type based on file extension.

Troubleshooting
===============
MimeDetect is distributed with a Magic database file to make FileInfo based MIME detection more consistent across servers.  This database file may not work with your server.  When this happens, you may see an error message like this in your Drupal status report:

"Fileinfo could not load the magic file. It could be corrupted. Try reinstalling the magic file distributed with the MimeDetect module. (Currently using Mime type detection PHP Fileinfo Extension)"

This message means that the included 'magic' database file will not work with your server configuration and  may have to use a different 'magic' file.  Usually there is a 'magic' file included in the File 4.x distribution, a PHP extension installed on your server.  

The first step to troubleshooting this problem is to determine if the extension is installed on your server, and then to learn the path to the 'magic' file.  You can contact your web host for this.  Then, in your settings.php, you can define a new variable that tells Mimedetect the correct and absolute path of the file to use:
  
  $conf = array(
    'mimedetect_magic' => '/usr/share/file/magic',
  );

The path '/usr/share/file/' is a common location for this file but you may have to replace that path with the correct magic file path if it is different on your server.  Some configurations will require you to use a file extension on the file name, such as '/usr/share/file/magic.mime'.

If this error message persists then you can create a PHP script to test that you have the correct path to the 'magic' database file.  Create a simple text file named test.php in your web root and call it in a browser.  Use this snippet in your script:

<?php
  $magic_file = '/usr/share/file/magic';
  $finfo = finfo_open(FILEINFO_MIME, $magic_file);
  if (!$finfo) {
    echo "Opening fileinfo database failed";
    exit();
  }
?>

Again, as mentioned above, the path will need to reflect the correct magic file path and you may need to add .mime to the file name.

seaneffel’s picture

Status: Needs work » Needs review
FileSize
3.49 KB

Bang. That, folks, is my first Drupal patch. Feels good.

Hope it works.

Maybe these changes are helpful to new users who don't know how to administer their servers. I teach Drupal classes and I see how frustrated users get when their docs don't speak their language - And, of course, how frustrated module maintainers are when their issue queues fill up.

drewish’s picture

Status: Needs review » Fixed

seaneffel, thanks, I added some line wrapping and committed this to HEAD and DRUPAL-6--1.

Status: Fixed » Closed (fixed)

Automatically closed -- issue fixed for 2 weeks with no activity.

chalkboard’s picture

On centos 5.6 and 6 at least, the right "magic" file seems to be
/usr/share/misc/magic.mgc

so in drupal/sites/default/settings.php :

$conf = array(
'mimedetect_magic' => '/usr/share/misc/magic.mgc',
);
should solve the problem

seaneffel’s picture

I fricking -hate- that magic file. If I could, I would either:

1. Make it vanish into thin air.
2. Put it in a box and saw it in half.

Seriously, if there were a single file in all of Drupal that I could nominate as the least magical - in fact sucks the magic out of the room - I would choose that one.

mlconnor’s picture

I'm running Redhat and the setup needs to be

$conf = array(
'mimedetect_magic' => '/usr/share/misc/magic.mgc',
);

abi2’s picture

I am still having this problem even after applying the patches.
I did a test outside drupal using the following php code:

$fp = finfo_open(FILEINFO_MIME);if (!$fp) { echo "Failed to open database\n"; exit(1);}echo $fp->file(__FILE__);
I got this error:
Warning: finfo_open() [function.finfo-open]: Failed to load magic database at '/usr/share/file/magic'. in /home/site/public_html/test.php on line 3
Opening fileinfo database failed

tobiberlin’s picture

I used the php file mentioned in README to find out if I defined the right path. Here this is the result:

Warning: finfo_open() [function.finfo-open]: open_basedir restriction in effect. File(/usr/share/file/magic.mime) is not within the allowed path(s): (/www/htdocs/xxx/:/tmp:/usr/bin:/www/htdocs/xxx:/bin:/usr/local/bin:/usr/share/php) in /www/htdocs/xxx/test.php on line 3
Opening fileinfo database failed
pmagunia’s picture

For me, the finfo_open function doesnt take a second parameter (https://bugs.php.net/bug.php?id=53452) The magic file is built into php so it doesnt need to be specificied (at least in php 5.3.)

Any ways I got it to work after changing line 55 in the install file to the following (remove the php brackets for you newbies)

if (!$finfo = @finfo_open(FILEINFO_MIME)) {

You will also need to change it in the module file if you are going the finfo route rather than the file binary

likewhoa’s picture

Version: 6.x-1.x-dev » 7.x-1.x-dev
Status: Closed (fixed) » Needs review
FileSize
1.19 KB

#42 WFM using php-5.3.10
attached is a patch to make these changes apply to -dev version and should only be used on systems with >=php-5.3.

caiosba’s picture

For Debian Squeeze, I had to add the following to settings.php:

$conf = array(
    'mimedetect_magic' => '/usr/share/file/magic.mgc',
    );  

And it went well. No patches needed.

aasarava’s picture

If you're on PHP >= 5.3 where the magic file is built into finfo_open, you can resolve the "could not load the magic file" error by adding the following to your settings.php file:

$conf = array(
  'mimedetect_magic' => '',
);
yogeshchaugule8’s picture

was having same issue with Drupal 6, #44 solved.

Yogesh

Justin Freeman’s picture

#45 PHP >= 5.3 solves problem Drupal 6.

dgtlmoon’s picture

The $conf edit worked for me, but I still think the patch is a better approach

esbon’s picture

Hi, thanks for the patch, but it did not work for me on D7 PHP 5.3.23 I have question though. Is PHP's fileinfo extension obsolete?

esbon’s picture

So the patch on #43 and the configuration on #45 are correct. In my case I had to delete fileinfo.ini from PHP.D directory so PHP did not try to load it giving me errors. This is working with PHP 5.3 Thanks everyone for posting this!

mattwmc’s picture

Having an issue getting this to work with Domain.

In settings.php, the coding seems to conflict with the two.

If I have don't have either one at the bottom, one won't work.

For instance if mimedetect is above domain, mimedetect won't work.

 $conf = array(
  'mimedetect_magic' => '',
);
include './modules/domain/settings.inc';  

Likewise, if domain is above mimedetect, my top level subdomains redirect to the main domain (i've tried an htaccess redirect, but doesn't work).

include './modules/domain/settings.inc';  
 $conf = array(
  'mimedetect_magic' => '',
);

Any ideas how to get the two codes to work correctly? Thanks.

Update: Got it to work.

Looks like it was an issue with memcache as well.

This seems to be working:

 $conf = array(
  'mimedetect_magic' => '',
);
# $conf['pressflow_smart_start'] = TRUE;
$conf['reverse_proxy'] = TRUE;
$conf['reverse_proxy_addresses'] = array('127.0.0.1');
 $conf = array(
   // The path to wherever memcache.inc is. The easiest is to simply point it
   // to the copy in your module's directory. 
   'cache_inc' => './modules/memcache/memcache.inc',  
 );
include './modules/domain/settings.inc';  
maximpodorov’s picture

Priority: Normal » Critical
Issue summary: View changes

This solution is quite fragile. The module must allow to select the way of mime type detection in settings form which must be shown always (it is hidden now if fileinfo is used). And the default way must be file_get_mimetype(). This will allow users to install the module and select the appropriate way for them. The settings form must allow to set 'mimedetect_magic' variable by hand, not by changing settings.php only.

manuel.adan’s picture

Status: Needs review » Fixed

New README.txt version (just committed) clarifies how to solve PHP fileinfo issues. As @mattwcm mentioned:

$conf['mimedetect_magic'] = '';

will let fileinfo extension relies on system or PHP builtin magic information.

If PHP fileinfo is not available after all, MimeDetect can work with the UNIX
'file' command. For security reasons, this must be explicitly enabled adding
this to your settings.php file:

$conf['mimedetect_enable_file_binary'] = TRUE;

and the file executable path if differs from the standard '/usr/bin/file':

$conf['mimedetect_file_binary'] = '/path/to/file_executable';

  • drewish committed 16abeae on 8.x-1.x
    #306217 by guix and drewish: Add variable to specify magic file path.
    
  • drewish committed a0fe0a4 on 8.x-1.x
    #306217 by seaneffel: Improve trouble shooting documentation.
    

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.