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


9

嗨,我正在尝试弄清楚为什么PHP在应为“ excel”时会返回“ msword”文件类型,因此已在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生成的。

谢谢你的帮助。

Answers:


7

file从中得到结果/etc/mime.types

从那里:

应用/数学
应用程序/ ms-tnef
应用程序/ msaccess mdb
应用程序/ msword文档点
应用程序/新闻消息ID
申请/新闻传播
应用/ ocsp请求
应用/ ocsp响应

mimetype从...获取

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它更准确。


看来我在Windows和Linux上都遇到问题了。感谢您的解释。
scampbell

是的,想一想... mimetype可能仅是台式机,因此在处理服务器时,您将使用文件。
Rinzwind

我使用的是12.04,看来编辑/etc/mime.types不会影响的结果file
congusbongus

5

两种.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.

看来我在Windows和Linux上都遇到问题了。感谢您的解释。抱歉,我不能将2标记为答案。两者都是现场。
scampbell

1

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

sudo apt-get install libfile-mimeinfo-perl

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


0

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

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

谢谢,我已经在使用此功能。返回错误的格式。这就是为什么我要测试。
scampbell

它返回什么格式?
llt

见原来的问题
scampbell

您没有提到您使用过finfo。它返回msword吗?
llt
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.