简体   繁体   English

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

[英]Mime type detection fails with fileinfo on PHP 5.3.8

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. 我在使用fileinfo检测简单PNG文件的mime类型时遇到问题,在CentOS服务器上安装了PHP 5.3.8。

The problem 问题

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);
?>

As you can see the file is a PNG image. 如您所见,该文件是PNG图像。 The header bytes of the file has been checked and are correct. 已检查文件的头字节并且是正确的。 But this page outputs an incorrect result : 但是此页面输出的结果不正确:

application/octet-stream

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. 我想这是因为无法检测到文件类型,fileinfo返回了默认响应,但我发现无法检查。

What I tried 我尝试了什么

To check the magic.mime file correctness, I used the file console command : 要检查magic.mime文件的正确性,我使用了文件控制台命令:

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

Which returned the expected result : 哪个返回了预期的结果:

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

I also tried another magic.mime file provided with Apache, but the problem remains. 我还尝试了Apache提供的另一个magic.mime文件,但问题仍然存在。

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). 我尝试通过默认的fileinfo位置(使用符号链接和文件的副本), MAGIC环境变量,以及在finfo_open调用中指定文件路径(如上所述)来指定finfo_open文件。

I tried updating PHP. 我试过更新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. 如果有人能帮助我,我会在太空中发出幸福的浪潮,这样他/她就可以幸福地生活。

Cheers 干杯

So why not call the file command from your script? 那么为什么不从脚本中调用file命令呢?

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

Sure, it's not perfect, but it's an option. 当然,它并不完美,但它是一种选择。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用Fileinfo在PHP中检测WebM MIME类型 - Detect WebM mime type in PHP with Fileinfo PHP中的错误MIME类型检测 - False MIME type detection in PHP 来自PHP fileinfo的错误/不正确的ogv MIME类型 - Incorrect/undesirable ogv MIME type from PHP fileinfo 使用PHP检查上传时的mime类型的文件。 mime_content_type,fileINfo,linux文件不可用 - Check mime type of file on upload with PHP. mime_content_type, fileINfo, linux file NOT available 在PHP中检测MIME类型失败 - Detecting a mime type fails in php PHP 5.3.5 fileinfo()MS Office 2007文件的MIME类型-magic.mime更新了吗? - PHP 5.3.5 fileinfo() MIME Type for MS Office 2007 files - magic.mime updates? Laravel“启用fileinfo扩展名后,由于没有可用的猜测器,因此无法猜测mime类型(是否启用了php_fileinfo扩展名?)” - Laravel “Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?)” when fileinfo extension is enabled 1/1)LogicException由于没有可用的猜测器,因此无法猜测mime类型(是否启用了php_fileinfo扩展名?) - 1/1) LogicException Unable to guess the mime type as no guessers are available (Did you enable the php_fileinfo extension?) 即使在启用 php_fileinfo 后,我也会收到错误:无法猜测 MIME 类型 - Even after enabling php_fileinfo I get error:unable to guess mime type PHP的FILEINFO_MIME选项返回空字符串 - PHP's FILEINFO_MIME option returns empty string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM