Finfo_file在上传的文件上以确定mime类型 [英] Finfo_file on uploaded file to determine mime-type

查看:122
本文介绍了Finfo_file在上传的文件上以确定mime类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定上传文件的mime类型,我想使用fileinfo(),这是我在尝试的,它不起作用:

Im trying to determine the mime-type of an uploaded file, i want to use fileinfo(), this is what ive been trying, it isnt working:

$uploadedfile = $_FILES['soup']['tmp_name'];
if(isset($uploadedfile))
{
    $uploadedname = $_FILES['soup']['name'];
    $file=$uploadedsong;
    $file.=$uploadedname;
    $finfo = finfo_open(FILEINFO_MIME_TYPE); 
    $mime = finfo_file($finfo, $file);

不幸的是,finfo_file似乎没有在运行,我假设我为此错误地设置了以下$file,有没有办法我可以像这样使用$_FILE正确处理新上传的文件?还是我要以完全不正确的方式解决这个问题.我直接使用另一个文件预先设置了一个文件,并且设置$file="folder/file.doc"正常工作.

Unfortunately the finfo_file doesnt seem to be running, Im assuming i have the following $file set incorrectly for this, is there a way i can do this properly with a newly uploaded file using $_FILE like this? or am i going at this problem the completely improper way. Using a file i have pre-set in another directly, and setting $file="folder/file.doc" works properly.

推荐答案

您应该将路径传递给finfo_file函数而不是文件名.

You should be passing the path to the finfo_file function not the filename.

<?php 
if (isset($_FILES['soup']['tmp_name'])) {
    $finfo = finfo_open(FILEINFO_MIME_TYPE);
    $mime = finfo_file($finfo, $_FILES['soup']['tmp_name']);
    if ($mime == 'application/msword') {
        //Its a doc format do something
    }
    finfo_close($finfo);
}
?>

这篇关于Finfo_file在上传的文件上以确定mime类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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