Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

Hi I am trying to work out why PHP is returning an 'msword' file type when it should be 'excel' so have tested on my Ubuntu box using both the 'file' and 'mimetype' commands. As you can see from the results below they return different results. Can anybody explain why?

root@dev:~# file --mime-type 1.xls
1.xls: application/msword
root@dev:~# mimetype 1.xls
1.xls: application/vnd.ms-excel

I should also note the file in question is returning 'msword' on a Windows Server. It was generated using PHPExcel.

Thanks for your help.

share|improve this question

4 Answers 4

up vote 4 down vote accepted

file gets its results from /etc/mime.types.

From there:

application/mathematica-old
application/ms-tnef
application/msaccess                            mdb
application/msword                              doc dot
application/news-message-id
application/news-transmission
application/ocsp-request
application/ocsp-response

mimetype gets it from ...

ENVIRONMENT

   XDG_DATA_HOME
   XDG_DATA_DIRS
       These variables can list base directories to search for data files.
       The shared mime-info will be expected in the "mime" sub directory
       of one of these directories. If these are not set, there will be
       searched for the following directories:

               $HOME/.local/share/mime
               /usr/local/share/mime
               /usr/share/mime

       See also the "XDG Base Directory Specification"
       http://freedesktop.org/Standards/basedir-spec
       <http://freedesktop.org/Standards/basedir-spec>

Worth noting is this part in the man page for mimetype:

For naming switches I followed the manpage of file(1) version 4.02 when possible. They seem to differ completely from the spec in the 'utilities' chapter of IEEE Std 1003.1-2001 (POSIX).

So you might call it a bug against file /etc/mime.types since vnd.ms-excel is more accurate.

share|improve this answer
    
Looks like I'm stuck with the problem then on both Windows and Linux. Thanks for the explanation. –  scampbell Apr 11 '13 at 7:06
    
yeah and thinking about it... mimetype is probably desktop only so when dealing with a server you are going to use file. –  Rinzwind Apr 11 '13 at 7:07

There is a similar difference between the two tools for .deb packages.

It appears that file uses /etc/mime.types and mimetype uses a much complicated mush of more XDG_DATA_DIRS (from man mimetypes):

ENVIRONMENT
       XDG_DATA_HOME
       XDG_DATA_DIRS
           These variables can list base directories to search for data files. The shared mime-info will be expected in the "mime" sub directory of one of these directories. If these are not set,
           there will be searched for the following directories:

                   $HOME/.local/share/mime
                   /usr/local/share/mime
                   /usr/share/mime

           See also the "XDG Base Directory Specification" http://freedesktop.org/Standards/basedir-spec <http://freedesktop.org/Standards/basedir-spec>

FILES
       The base dir for all data files is determined by two environment variables, see "ENVIRONMENT".

       BASE/mime/packages/SOURCE.xml
           All other files are compiled from these source files. To re-compile them use update-mime-database(1).

       BASE/mime/globs
           Compiled information about globs.

       BASE/mime/magic
           Compiled information about magic numbers.

       BASE/mime/MEDIA/SUBTYPE.xml
           Descriptions of a mimetype in multiple languages, used for the "--describe" switch.
share|improve this answer
    
Looks like I'm stuck with the problem then on both Windows and Linux. Thanks for the explanation. Apologies I cant mark 2 as the anwser. Both were spot on. –  scampbell Apr 11 '13 at 7:07

You mentioned you're doing it in PHP. I encountered such differences when getting the mime type of file. They do differ. To properly do it in PHP, you should use finfo

$finfo = new finfo();
$mime = $finfo->file($path_to_file, FILEINFO_MIME);
share|improve this answer
    
Thanks I am already using this function. It returns the incorrect format. This is why I was testing. –  scampbell Apr 11 '13 at 7:04
    
what format does it return? –  llt May 22 '13 at 7:47
    
See original question –  scampbell May 22 '13 at 12:00
    
You didn't mention there you used finfo. Does it return msword? –  llt May 23 '13 at 4:54

It took me a while to find it, but I was able to install mimetype on debian.

sudo apt-get install libfile-mimeinfo-perl

Now I get application/vnd.ms-excel for a .xls instead of application/msword.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.