douzhi9478 2011-07-18 05:48
浏览 9

检查文件类型?

I need to check a file's type on upload to make sure it's an image. What I'm thinking of doing is checking the MIME file type(from $_FILE) and then checking that against the extension. Something like:

function checkType($file){

    $ext = pathinfo($_FILES[$file]['name'], PATHINFO_EXTENSION);
    $mime = $_FILES[$file]['type'];

    if($mime == "image/jpg" || $mime == "image/jpeg"){

        if($ext == "jpg" || $ext == "jpeg"){
            return true;
        } else{
            return false;
        }

    }

    if($mime == "image/gif"){

        if($ext == "gif"){
            return true;
        } else{
            return false;
        }

    }

    if($mime == "image/png"){

        if($ext == "png"){
            return true;
        } else{
            return false;
        }

    }

}

Is this a good approach? Suggestions?

Also, how would I go about getting the width of the image before it's finally uploaded to the server?

  • 写回答

2条回答 默认 最新

  • dougu1990 2011-07-18 05:56
    关注

    No, its bad. Mime type could not match actual file contents. To be sure what kind of uploaded file you received (or if its actually an image) you need to check file contents.

    Try function like getimagesize for example. If it will return valid data then you have an image.

    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集