严重的fileinfo问题 [英] Severe fileinfo issues

查看:94
本文介绍了严重的fileinfo问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在共享主机上,并且一直在与他们进行斗争以使fileinfo正常运行,我们终于使它正常运行,但是我又遇到了另一个障碍,我本质上得到了这个,因为我正在创建一个新的文件上传工具,我们需要知道正在上传哪些模仿类型,但是fileinfo不想玩.

I am on shared hosting, and have been battling with them to get fileinfo working, we have finally got it working, but again I have been hit with another barrier, I have essentially got this because I am creating a new file upload tool and we need to know what mimetypes are being uploaded, but fileinfo does not want to play ball.

因此,.sql文件将返回text/plain,正确.

So, a .sql file will return text/plain, correct.

但是,每个其他文件都会简单地返回application/octet-stream,我想知道为什么会这样,我不想让主机烦恼成堆的问题,所以我想进行一些研究在我让他们烦恼之前解决这个问题.

However, every other file will simply return application/octet-stream, what I am wondering is why this is the case, I don't want to be pissing the host off with tons of questions so I want to get some research on the issue before I hassle them furthur.

代码:

function get_mime($filename)
{
    $result = new finfo(FILEINFO_MIME_TYPE, "/usr/share/file/magic.mime"); 
    return $result->file($filename, FILEINFO_MIME_TYPE);
}

echo $user->get_mime($_FILES['file']['tmp_name'][$i]);

任何帮助将不胜感激,非常感谢

Any help would be greatly appreciated, many thanks

因此,我打算更新MIMETYPE检查的代码,并且已经更新了代码以反映以下内容(针对该功能):

So I have gone about updating the code of the MIMETYPE check, and I have updated the code to reflect the following(for the function):

function get_mime($filename)
{
    $result = new finfo(FILEINFO_MIME_TYPE, "/usr/share/file/magic.mime"); // return mime type ala mimetype extension

    if (is_resource($result) === true)
    {
        return $result->file($filename, FILEINFO_MIME_TYPE);
    } else {
        return "failed";
    }

    return false;
}

很明显,正如您所看到的,我使它在失败时输出 failed .这是100%的时间,并且删除if语句导致它返回application/octet-stream,这是不正确的.

Obviously, as you can see I have made it output failed when it, fails. This is happening 100% of the time, and removing that if statement causes it to return application/octet-stream, which is incorrect.

上传过程将是:用户上传->文件被移动到公共目录上方的临时文件夹(upload_check),在该目录中,fileinfo将对其进行检查,如果不是我们接受的文件,则将其丢弃,否则它将被丢弃.复制到公用文件,然后从临时文件夹中丢弃.

The upload process will be: User uploads -> Files are moved to a temporary folder above public, (upload_check) where they will be checked by fileinfo, if the file is not what we accept it will be discarded, else it will be copied to the public files and and then discarded from the temporary folder.

我已经创建了该进程,但fileinfo仍然不希望合作并且返回所有内容的application/octet-stream,即使它们只是保存在public_html文件夹上方的服务器上.

I have created that process, and still fileinfo doesn't want to co-operate and returns application/octet-stream for everything, even though they are on the server just saved above the public_html folder.

有什么建议吗?

杰克

推荐答案

建议

我真的认为您在这里弄错了...

Advice

I really think you are getting things wrong here ...

A.上传文件时,它还会在$_FILES

A. when a file is uploaded it also returns mime type automatically in the $_FILES

echo $_FILES['file']['type'][$i] ;

B如果要获取未上载文件的mime,或尝试更好地确定客户端标头提供的内容,请使用mime_content_type

B if you want to get mime for a file that was not uploaded, or try and make a better determination that what the client headers provided, use mime_content_type http://php.net/manual/en/function.mime-content-type.php

echo mime_content_type($fileName);

C.折旧

mime_content_type& Fileinfo维护不正确,已被贬值.

Both mime_content_type & Fileinfo are not properly maintained and they have been depreciated.

更新:该手册曾经被错误地标记为已弃用,但已得到修复 https://bugs.php.net/bug.php?id=71367 于2016年1月14日发布.可以看到它支持7.0,所以根本不会停产.

Update: It once was incorrectly marked as deprecated in the manual, but it has been fixed https://bugs.php.net/bug.php?id=71367 on the 14th of January 2016. As can see supports 7.0 so not discontinued at all.

或者可以建议您查看以下链接进行替换

Alternatively can suggest you look at the following links for replacement

获取压缩文件的mime类型

http://www.php.net /manual/zh-CN/function.mime-content-type.php#87856

D.您还可以使用getID3 http: //sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.9.3/可以检查某些常见文件的真实mime类型...

D. You can also use getID3 http://sourceforge.net/projects/getid3/files/getID3%28%29%201.x/1.9.3/ which can check the real mime type for some common files ...

E.使用Linux命令行保护程序

E. Using Linux Command line probably saver

$mime = shell_exec("file -bi " . escapeshellarg($file));   

或者在Mac上

$mime = shell_exec("file -b --mime ".escapeshellarg($file));

最后

为什么要直接在$_FILES['file']['tmp_name'][$i]上工作?已知这会带来很多问题

Finally

Why do you want to work on $_FILES['file']['tmp_name'][$i] directly ?? This has been know to give so many issues

尝试

$tempDir = ""; // Temp
foreach ( $_FILES ["file"] ["error"] as $key => $error ) {
    if ($error == UPLOAD_ERR_OK) {
        $tmp_name = $_FILES ["file"] ["tmp_name"] [$key];
        $name = $_FILES ["file"] ["name"] [$key];
        $tempFile = "$tempDir/$name";
        move_uploaded_file ( $tmp_name, $tempFile );
        echo $user->get_mime ( $tempFile );
        unlink ( $tempFile );

    }
}

这篇关于严重的fileinfo问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆