php 7.2 finfo 魔术文件
php 7.2 finfo magic file
我有一个 Laravel 5 项目,让用户下载 .ai(插画文件)。问题是 Laravel 将 .ai 文件检测为 application/pdf.
我正在使用此功能检测 mime 类型
$type = File::mimeType( $_path );
我也尝试过使用这种方法,但得到了相同的结果
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = $finfo->file($_path);
finfo_close($finfo);
我想,这一定是 PHP 根本不知道 .ai 文件是什么的问题。我深入研究了 finfo,我知道默认的 mime 定义被编译成 PHP,但我看到 finfo_open 有第二个参数 'magic_file',我认为这是一个你可以插入不同 MIME 定义文件的路径。
我尝试使用 Ubuntu 的 /etc/magic.mime 文件,但 finfo 给了我
ErrorException: finfo_open(): Warning: offset `application\/activemessage' invalid in
错误。我认为这是因为 magic.mime 文件的格式不正确。
大多数在线主题都创建了自定义 PHP 函数或其他一些 hack 来检测 mime 类型,但我觉得这不是正确的解决方案。
在哪里可以找到最新的 MIME 定义文件以及如何将它们加载到 PHP 或 finfo 中?
我的环境:
Ubuntu 16.04
PHP 7.2
I figured, this has to be an issue with PHP simply not knowing what an
.ai file is. I dug deeper into finfo and I understand that the default
mime definitions are compiled into PHP, but I see that finfo_open, has
a second argument 'magic_file', which I assume is a place that you can
insert the path to a different mime definition file.
fileinfo extension 尝试通过在文件中的特定位置寻找特定的 magic 序列来猜测 mime 类型。魔法文件是一个数据库,存储了已知的魔法序列。
I have a Laravel 5 project, which let's an user download .ai
(illustrator files). The issue is that Laravel detects .ai files as
application/pdf.
按照上面的解释,寻找魔法文件检测.ai
个文件为pdf是可以的。因为Adobe Illustrator Artwork是可以保存为EPS或PDF格式的文件。
为了区分一般的pdf文件和以pdf格式保存的ai文件,我做了一些研究。首先,我从互联网上下载了免费的 ai 文件,通过使用命令 hexdump
和 file
.
查看 magic number 来识别文件
$ hexdump -C 7_full_ai_vi_template_vector_8.ai | head
00000000 25 50 44 46 2d 31 2e 34 0d 25 e2 e3 cf d3 0d 0a |%PDF-1.4.%......|
00000010 31 20 30 20 6f 62 6a 0d 3c 3c 20 0d 2f 54 79 70 |1 0 obj.<< ./Typ|
00000020 65 20 2f 43 61 74 61 6c 6f 67 20 0d 2f 50 61 67 |e /Catalog ./Pag|
00000030 65 73 20 32 20 30 20 52 20 0d 2f 4d 65 74 61 64 |es 2 0 R ./Metad|
00000040 61 74 61 20 38 38 20 30 20 52 20 0d 3e 3e 20 0d |ata 88 0 R .>> .|
00000050 65 6e 64 6f 62 6a 0d 32 20 30 20 6f 62 6a 0d 3c |endobj.2 0 obj.<|
00000060 3c 20 0d 2f 54 79 70 65 20 2f 50 61 67 65 73 20 |< ./Type /Pages |
00000070 0d 2f 4b 69 64 73 20 5b 20 35 20 30 20 52 20 5d |./Kids [ 5 0 R ]|
00000080 20 0d 2f 43 6f 75 6e 74 20 31 20 0d 3e 3e 20 0d | ./Count 1 .>> .|
00000090 65 6e 64 6f 62 6a 0d 33 20 30 20 6f 62 6a 0d 3c |endobj.3 0 obj.<|
$ file 7_full_ai_vi_template_vector_8.ai
7_full_ai_vi_template_vector_8.ai: PDF document, version 1.4
查看文件的前几个字节,如您所见,它是扩展名为 .ai
的 PDF 文件。
之后,我在 Mac 上用 Preview 打开它,它知道这个文件是由 Adobe Illustrator 在 Inspector Dialog 中创建的。所以它一定有办法找出以PDF格式保存的AI文件。
Most of the topics online create a custom PHP function or some other
hack to detect mime types, but I feel as if that is not the correct
solution here.
Where can I find up-to-date mime definition files and how can I load
them into PHP or finfo?
我在谷歌上搜索了解决方案,但没有成功,所以我自己创建了它,在 Adobe Illustrator 文件的第 15 页
格式规范,它说:
The %%Creator comment identifies the application that generated the
PostScript language document. The version number (version 6.0 in
Figure 1) is arbitrary text, terminated by a newline character.
我假设文件开头包含 pdf 魔术字节和字符串 %%Creator Adobe Illustrator
的文件应标识为 .ai
。
让我们写一些 magic rule:
$ cat ai
0 string %PDF- PDF document
!:mime application/pdf
>5 byte x \b, version %c
>7 byte x \b.%c
>7 search/1000 %%Creator:\ Adobe\ Illustrator Adobe Illustrator Document
PHP 脚本为 .ai
使用自定义魔术文件
$ cat fileinfo.php
<?php
$magic_file = __DIR__ . '/ai';
$finfo = new finfo(FILEINFO_NONE, $magic_file);
echo $finfo->file($argv[1]) . PHP_EOL;
会输出
$ php fileinfo.php ./7_full_ai_vi_template_vector_8.ai
PDF document, version 1.4 Adobe Illustrator Document
它有效,但我认为维护您自己的魔法文件不是个好主意。也许你可以为它写一个简单的函数,Detect .ai
after $type = File::mimeType( $_path );
says it is pdf file.
我有一个 Laravel 5 项目,让用户下载 .ai(插画文件)。问题是 Laravel 将 .ai 文件检测为 application/pdf.
我正在使用此功能检测 mime 类型
$type = File::mimeType( $_path );
我也尝试过使用这种方法,但得到了相同的结果
$finfo = finfo_open(FILEINFO_MIME);
$mimetype = $finfo->file($_path);
finfo_close($finfo);
我想,这一定是 PHP 根本不知道 .ai 文件是什么的问题。我深入研究了 finfo,我知道默认的 mime 定义被编译成 PHP,但我看到 finfo_open 有第二个参数 'magic_file',我认为这是一个你可以插入不同 MIME 定义文件的路径。
我尝试使用 Ubuntu 的 /etc/magic.mime 文件,但 finfo 给了我
ErrorException: finfo_open(): Warning: offset `application\/activemessage' invalid in
错误。我认为这是因为 magic.mime 文件的格式不正确。
大多数在线主题都创建了自定义 PHP 函数或其他一些 hack 来检测 mime 类型,但我觉得这不是正确的解决方案。
在哪里可以找到最新的 MIME 定义文件以及如何将它们加载到 PHP 或 finfo 中?
我的环境:
Ubuntu 16.04
PHP 7.2
I figured, this has to be an issue with PHP simply not knowing what an .ai file is. I dug deeper into finfo and I understand that the default mime definitions are compiled into PHP, but I see that finfo_open, has a second argument 'magic_file', which I assume is a place that you can insert the path to a different mime definition file.
fileinfo extension 尝试通过在文件中的特定位置寻找特定的 magic 序列来猜测 mime 类型。魔法文件是一个数据库,存储了已知的魔法序列。
I have a Laravel 5 project, which let's an user download .ai (illustrator files). The issue is that Laravel detects .ai files as application/pdf.
按照上面的解释,寻找魔法文件检测.ai
个文件为pdf是可以的。因为Adobe Illustrator Artwork是可以保存为EPS或PDF格式的文件。
为了区分一般的pdf文件和以pdf格式保存的ai文件,我做了一些研究。首先,我从互联网上下载了免费的 ai 文件,通过使用命令 hexdump
和 file
.
$ hexdump -C 7_full_ai_vi_template_vector_8.ai | head
00000000 25 50 44 46 2d 31 2e 34 0d 25 e2 e3 cf d3 0d 0a |%PDF-1.4.%......|
00000010 31 20 30 20 6f 62 6a 0d 3c 3c 20 0d 2f 54 79 70 |1 0 obj.<< ./Typ|
00000020 65 20 2f 43 61 74 61 6c 6f 67 20 0d 2f 50 61 67 |e /Catalog ./Pag|
00000030 65 73 20 32 20 30 20 52 20 0d 2f 4d 65 74 61 64 |es 2 0 R ./Metad|
00000040 61 74 61 20 38 38 20 30 20 52 20 0d 3e 3e 20 0d |ata 88 0 R .>> .|
00000050 65 6e 64 6f 62 6a 0d 32 20 30 20 6f 62 6a 0d 3c |endobj.2 0 obj.<|
00000060 3c 20 0d 2f 54 79 70 65 20 2f 50 61 67 65 73 20 |< ./Type /Pages |
00000070 0d 2f 4b 69 64 73 20 5b 20 35 20 30 20 52 20 5d |./Kids [ 5 0 R ]|
00000080 20 0d 2f 43 6f 75 6e 74 20 31 20 0d 3e 3e 20 0d | ./Count 1 .>> .|
00000090 65 6e 64 6f 62 6a 0d 33 20 30 20 6f 62 6a 0d 3c |endobj.3 0 obj.<|
$ file 7_full_ai_vi_template_vector_8.ai
7_full_ai_vi_template_vector_8.ai: PDF document, version 1.4
查看文件的前几个字节,如您所见,它是扩展名为 .ai
的 PDF 文件。
之后,我在 Mac 上用 Preview 打开它,它知道这个文件是由 Adobe Illustrator 在 Inspector Dialog 中创建的。所以它一定有办法找出以PDF格式保存的AI文件。
Most of the topics online create a custom PHP function or some other hack to detect mime types, but I feel as if that is not the correct solution here.
Where can I find up-to-date mime definition files and how can I load them into PHP or finfo?
我在谷歌上搜索了解决方案,但没有成功,所以我自己创建了它,在 Adobe Illustrator 文件的第 15 页 格式规范,它说:
The %%Creator comment identifies the application that generated the PostScript language document. The version number (version 6.0 in Figure 1) is arbitrary text, terminated by a newline character.
我假设文件开头包含 pdf 魔术字节和字符串 %%Creator Adobe Illustrator
的文件应标识为 .ai
。
让我们写一些 magic rule:
$ cat ai
0 string %PDF- PDF document
!:mime application/pdf
>5 byte x \b, version %c
>7 byte x \b.%c
>7 search/1000 %%Creator:\ Adobe\ Illustrator Adobe Illustrator Document
PHP 脚本为 .ai
$ cat fileinfo.php
<?php
$magic_file = __DIR__ . '/ai';
$finfo = new finfo(FILEINFO_NONE, $magic_file);
echo $finfo->file($argv[1]) . PHP_EOL;
会输出
$ php fileinfo.php ./7_full_ai_vi_template_vector_8.ai
PDF document, version 1.4 Adobe Illustrator Document
它有效,但我认为维护您自己的魔法文件不是个好主意。也许你可以为它写一个简单的函数,Detect .ai
after $type = File::mimeType( $_path );
says it is pdf file.