简体   繁体   English

PHP的FILEINFO_MIME选项返回空字符串

[英]PHP's FILEINFO_MIME option returns empty string

I'm trying to check the MIME type of an uploaded file in my PHP application. 我正在尝试检查PHP应用程序中上载文件的MIME类型。 I upload the file, then do this, where $file is the path to my file: 我上传文件,然后执行此操作,其中$ file是我的文件的路径:

$finfo = new finfo(FILEINFO_MIME);
$mimetype = $finfo->file($file);

In this situation, $mimetype is always an empty string. 在这种情况下,$ mimetype始终是一个空字符串。 I've tested on several file types (.jpg, .doc, .txt, .pdf) and it's always empty. 我已经测试了几种文件类型(.jpg,.doc,.txt,.pdf),但始终为空。 It's supposed to return something like "image/jpeg". 它应该返回类似“ image / jpeg”的内容。

I was debugging and changed the first line so that the code snippet is now this: 我正在调试并更改了第一行,因此代码段现在是这样的:

$finfo = new finfo(FILEINFO_NONE);
$info = $finfo->file($file);

In this situation, when I uploaded a jpg, $info was this: JPEG image data, JFIF standard 1.02. 在这种情况下,当我上载jpg时,$ info是:JPEG图像数据,JFIF标准1.02。 So now I know it's getting to the file correctly, but passing in FILEINFO_MIME doesn't give me back the correct mime string. 因此,现在我知道它正确到达了文件,但是传入FILEINFO_MIME并不能给我正确的mime字符串。

This only happens on my staging server. 这仅在我的登台服务器上发生。 On my local server, I get the correct mime type. 在本地服务器上,我得到了正确的mime类型。 Does anyone have any ideas why my staging server returns an empty string for mime type? 有谁知道为什么我的登台服务器返回一个空字符串作为mime类型?

I'm wondering if the magic file is correctly placed on your server. 我想知道魔术文件是否正确放置在您的服务器上。

magic_file magic_file
Name of a magic database file, usually something like /path/to/magic.mime. 魔术数据库文件的名称,通常类似于/path/to/magic.mime。 If not specified, the MAGIC environment variable is used. 如果未指定,则使用MAGIC环境变量。 If this variable is not set either, /usr/share/misc/magic is used by default. 如果也未设置此变量,则默认使用/ usr / share / misc / magic。 A .mime and/or .mgc suffix is added if needed. 如果需要,将添加.mime和/或.mgc后缀。

Since you can specify your own file Via the last argument 由于您可以通过最后一个参数指定自己的文件

$finfo = new finfo(FILEINFO_MIME, "/usr/share/misc/magic");

Did you try the example from the manual : 您是否尝试过手册中的示例:

<?php
$finfo = finfo_open(FILEINFO_MIME_TYPE); // return mime type ala mimetype extension

   echo finfo_file($finfo, $filename) . "\n";

finfo_close($finfo);
?>

Try this: 尝试这个:

<?php
$fi = new finfo(FILEINFO_MIME,'/usr/share/file/magic');
$mime_type = $fi->buffer(file_get_contents($file));
?>

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM