‘file –mime-type’和’mimetype’命令返回不同的结果

嗨,我想弄清楚为什么PHP返回’msword’文件类型,当它应该是’excel’所以我已经使用’file’和’mimetype’命令在我的Ubuntu盒子上进行了测试。 从下面的结果中可以看出,它们会返回不同的结果。 任何人都可以解释原因吗?

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生成的。

谢谢你的帮助。

file/etc/mime.types获取结果。

从那里:

应用程序/数学老
应用程序/ MS-TNEF
 application / msaccess mdb
 application / msword doc dot
应用程序/新闻消息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  

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

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

所以你可能会把它称为文件/etc/mime.types的错误,因为vnd.ms-excel更准确。

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

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

 sudo apt-get install libfile-mimeinfo-perl 

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

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

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