Mime类型检测失败与PHP

Mime类型检测失败与PHP

本文介绍了Mime类型检测失败与PHP 5.3.8上的fileinfo的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用fileinfo检测简单PNG文件的mime类型时遇到问题,在CentOS服务器上安装了PHP 5.3.8。





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

 <?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图像。文件的头字节已检查并且正确。但此页面输出的结果不正确:

  application / octet-stream 
pre>

我想这是因为文件类型不能被检测到,fileinfo返回默认响应,但我没有办法检查。



我尝试的



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

这返回了预期结果:

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


$ b b

我也尝试过使用Apache提供的另一个magic.mime文件,但问题仍然存在。



我尝试通过默认的fileinfo位置指定magic.mime文件与文件的符号链接和副本),与 MAGIC 环境变量,并通过指定文件路径在 finfo_open



...现在我离开了






如果任何人可以帮助我,我会通过太空发送幸福的浪花,让他/她



干杯

解决方案

从脚本中调用文件命令?

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


$ b b

当然,这不是完美的,但它是一个选择。


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.

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

What I tried

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

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.

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

I tried updating 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?

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

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

这篇关于Mime类型检测失败与PHP 5.3.8上的fileinfo的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-28 19:26