[英]Add MIME type knowledge to PHP
I know of the UNIX command file
to detect the mime type of a file, which I can execute via PHP (like here ): 我知道用于检测
file
的mime类型的UNIX命令file
,我可以通过PHP执行(如此处所示 ):
$content_type = exec("file -bi " . escapeshellarg($filepath));
And I am also aware of 我也知道
$fi = new finfo();
echo $fi->file($filename, FILEINFO_MIME_TYPE) . PHP_EOL;
and 和
echo mime_content_type($filename) . PHP_EOL;
Both PHP built-in solutions produce the same output. 两种PHP内置解决方案都可以产生相同的输出。 However, the Linux
file
command knows some formats more. 但是,Linux
file
命令更多地了解某些格式。
AMR audio files were reported as application/octet-stream
with all three methods. 使用所有三种方法将AMR音频文件报告为
application/octet-stream
。 Then I added the file magic to the database of the file
command: 然后我将文件魔法添加到
file
命令的数据库中:
$ cat /etc/magic
0 string #!AMR\n Adaptive Multi-Rate Audio Codec
!:mime audio/amr
$ file -bi test115.amr
audio/amr; charset=binary
However, PHP still reports application/octet-stream
. 但是,PHP仍会报告
application/octet-stream
。
I thought the PHP-builtin and file
use the same database somehow. 我认为PHP内置
file
以某种方式使用相同的数据库。 How can I train PHP to know the MIME type of an AMR file? 如何训练PHP以了解AMR文件的MIME类型?
PHP seems to use their own, bundled database for fileinfo lookups; PHP似乎使用自己的捆绑数据库进行文件信息查找; you can override this behaviour by either adding the second parameter to the
new finfo
call, or by setting the MAGIC
env var. 您可以通过将第二个参数添加到
new finfo
调用或通过设置MAGIC
env var来覆盖此行为。
From the docs: 来自文档:
Note: Generally, using the bundled magic database (by leaving magic_file and the MAGIC environment variables unset) is the best course of action unless you specifically need a custom magic database.
注意:通常,使用捆绑的魔术数据库(通过保留magic_file和MAGIC环境变量未设置)是最佳操作,除非您特别需要自定义魔术数据库。
http://php.net/manual/en/function.finfo-open.php http://php.net/manual/en/function.finfo-open.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.