'file --mime-type' 和 'mimetype' 命令返回不同的结果

'file --mime-type' 和 'mimetype' 命令返回不同的结果

嗨,我想弄清楚为什么 PHP 返回的是“msword”文件类型,而它应该是“excel”,因此在我的 Ubuntu 机器上使用“file”和“mimetype”命令进行了测试。从下面的结果可以看出,它们返回的结果不同。有人能解释一下为什么吗?

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

我还应该注意到,有问题的文件在 Windows Server 上返回“msword”。它是使用 PHPExcel 生成的。

感谢您的帮助。

答案1

file从 获得其结果/etc/mime.types

从那里:

应用程序/mathematica-old
应用程序/ms-tnef
应用程序/msaccess mdb
应用程序/msword doc 点
应用程序/新闻消息 ID
应用/新闻传输
应用程序/ocsp 请求
应用程序/ocsp 响应

模仿类型从...得到

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>

值得注意的是 mimetype 手册页中的这部分:

对于命名开关,我尽可能遵循 file(1) 版本 4.02 的手册页。它们似乎与 IEEE Std 1003.1-2001 (POSIX) 的“实用程序”一章中的规范完全不同。

因此您可能将其称为文件错误,/etc/mime.types因为vnd.ms-excel这样更准确。

答案2

这两个包工具之间也有类似的差异.deb

出现该文件使用/etc/mime.typesmimetype使用了更多更复杂的内容XDG_DATA_DIRS(来自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.

答案3

我花了一段时间才找到它,但我能够在 debian 上安装 mimetype。

sudo apt-get install libfile-mimeinfo-perl

现在我得到的application/vnd.ms-excel是 .xls 而不是application/msword

答案4

您提到您是在 PHP 中执行此操作。我在获取文件的 mime 类型时遇到了这样的差异。它们确实不同。要在 PHP 中正确执行此操作,您应该使用 finfo

$finfo = new finfo();
$mime = $finfo->file($path_to_file, FILEINFO_MIME);

相关内容