• php 上传文件并对上传的文件进行简单验证(错误信息,格式(防伪装),大小,是否为http上传)


    <body>
    <?php
    /**
    *验证错误
    *如果有错,就返回错误,如果没错,就返回null
    */
    function check($file) {
        //1:验证是否有误
        if($file['error']!=0){
            switch($file['error']) {
                case 1:
                    return '文件大小超过了php.ini中允许的最大值,最大值是:'.ini_get('upload_max_filesize');
                case 2:
                    return '文件大小超过了表单允许的最大值';
                case 3:
                    return '只有部分文件上传';
                case 4:
                    return '没有文件上传';
                case 6:
                    return '找不到临时文件';
                case 7:
                    return '文件写入失败';
                default:
                    return '未知错误';
            }
        }
        //2、验证格式
       //第一步:创建finfo资源
    $info=finfo_open(FILEINFO_MIME_TYPE);
    //第二步:将finfo资源和文件做比较
    $mime=finfo_file($info,$file['tmp_name']);
       //第三步,比较是否合法
    $allow=array('image/jpeg','image/png','image/gif'); //允许的类别 if(!in_array($mime,$allow)){ return '只能上传'.implode(',',$allow).'格式'; } //3、验证大小 $size=123456789; if($file['size']>$size){ return '文件大小不能超过'.number_format($size/1024,1).'K'; } //4、验证是否是http上传 if(!is_uploaded_file($file['tmp_name'])) return '文件不是HTTP POST上传的<br>'; return null; //没有错误 } //表单提交 if(!empty($_POST)) { //上传文件过程中有错误就显示错误 if($error=check($_FILES['face'])){ echo $error; }else{ //文件上传,上传的文件保存到当天的文件夹中 $foldername=date('Y-m-d'); //文件夹名称 $folderpath="./uploads/{$foldername}"; //文件夹路径 if(!is_dir($folderpath)) mkdir($folderpath); $filename=uniqid('',true).strrchr($_FILES['face']['name'],'.'); //文件名 $filepath="$folderpath/$filename"; //文件路径 if(move_uploaded_file($_FILES['face']['tmp_name'],$filepath)) echo "上传成功,路径是:{$foldername}/{$filename}"; else echo '上传失败<br>'; } } ?> <form method="post" action="" enctype='multipart/form-data'> <input type="file" name="face"> <input type="submit" name="button" value="上传"> </form> </body>

    运行结果

    防治文件伪装需要在php.ini中开启fileinfo.dll扩展,开启fileinfo扩展以后,就可以使用finfo_*的函数。

  • 相关阅读:
    ES6常用语法
    @Autowired和@Resource的区别
    spring boot 引导
    Springboot 日志管理配置logback-spring.xml
    Java异常之checked与unchecked
    @Transactional(rollbackFor=Exception.class)的使用
    使用spring @Scheduled注解执行定时任务、
    Spring入门学习推荐
    Hadoop调优
    关于Flink--ProcessFunction的使用
  • 原文地址:https://www.cnblogs.com/ruoruchujian/p/11120761.html
Copyright © 2020-2023  润新知