Mime 类型检测在 PHP 5.3.8 上使用 fileinfo 失败

2022-01-13 00:00:00 centos php fileinfo

在 CentOS 服务器上安装 PHP 5.3.8 的情况下,我无法使用 fileinfo 检测简单 PNG 文件的 mime 类型.

I'm having trouble detecting the mime type of a simple PNG file with fileinfo, with PHP 5.3.8 installed on a CentOS server.

问题

基本上,如果我有以下代码:

Basically, if I have the following code :

<?php
$const = defined('FILEINFO_MIME_TYPE') ? FILEINFO_MIME_TYPE : FILEINFO_MIME;
$handle = finfo_open($const, '/usr/share/file/magic.mime');
$result = finfo_file($handle, '/var/vhosts/v4dev/public/Melvin.png');
echo $result;
unset($handle);
?>

如您所见,该文件是 PNG 图像.文件的头字节已被检查并且是正确的.但是这个页面输出了一个不正确的结果:

As you can see the file is a PNG image. The header bytes of the file has been checked and are correct. But this page outputs an incorrect result :

application/octet-stream

我猜这是因为无法检测到文件类型,而 fileinfo 返回了默认响应,但我没有办法检查.

I guess this is because the file type could not be detected, and fileinfo returned the default response, but I found no way to check that.

我尝试了什么

为了检查 magic.mime 文件的正确性,我使用了文件控制台命令:

To check the magic.mime file correctness, I used the file console command :

file -m /usr/share/file/magic.mime /var/vhosts/v4dev/public/Melvin.png

返回了预期的结果:

/var/vhosts/v4dev/public/Melvin.png: image/png

我还尝试了 Apache 提供的另一个 magic.mime 文件,但问题仍然存在.

I also tried another magic.mime file provided with Apache, but the problem remains.

我尝试通过默认文件信息位置(带有符号链接和文件副本)、MAGIC 环境变量以及在 中指定文件路径来指定 magic.mime 文件finfo_open 调用(如上).

I tried specifying the magic.mime file through the default fileinfo location (with symlink and copy of the file), with the MAGIC environment variable, and by specifying the file path in the finfo_open call (as above).

我尝试更新 PHP.

...现在我别无选择.

... and now i'm out of options.

如果有人能帮我解决这个问题,我会在太空中发送幸福的波浪,这样他/她就可以从此过上幸福的生活.

If anyone could help me with this, i'd send waves of happiness through space so he/her can live happily ever after.

干杯

推荐答案

那么为什么不从你的脚本中调用 file 命令呢?

So why not call the file command from your script?

$result = `file -bm /usr/share/file/magic.mime /var/vhosts/v4dev/public/Melvin.png`

当然,它并不完美,但它是一种选择.

Sure, it's not perfect, but it's an option.

相关文章