php文件上传

max_size = $max_size;
        // 允许上传的文件类型
        $this->mime_allowed = $mime_allowed;
    }

    public function upload($input_name,$base_path = BASEPATH.'/'.'uploads')
    {
        if (!empty($input_name)) {
            $file_info = [];
            $file_flag = 0;
            $file_error = '';
            // 上传文件类型
            $count_mime_allowed = count($this->mime_allowed);
            // 上传文件数
            if ($_FILES[$input_name]['tmp_name']) {
                $file_counts = count($_FILES[$input_name]['tmp_name']);
                for ($i=0; $i < $file_counts; $i++) { 
                    if (is_uploaded_file($_FILES[$input_name]['tmp_name'][$i])) {
                        $file_type = pathinfo($_FILES[$input_name]['name'][$i], PATHINFO_EXTENSION);
                        $file_name = date('YmdHis').mt_rand(10000, 99999).'.'. $file_type;
                        //文件大小
                        $file_size = $_FILES[$input_name]['size'][$i];
                        if ($file_size>$this->max_size) {
                            $file_error.='The file is too large'; 
                            continue;
                        }
                        $new_path = $base_path . '/' .$file_type.'/'.date('Ym');
                        // 文件路径
                        $web_path = $file_type.'/'.date('Ym');
                        if (!file_exists($new_path)) {
                            mkdir($new_path,0755,true);
                        }
                        //文件存储路径
                        //文件信息
                        $file_info[$i] = array(
                            'old_name'=> $_FILES[$input_name]['name'][$i],
                            'file_name' => $file_name,
                            'file_size' => $file_size,
                            'mime' => finfo_file(finfo_open(FILEINFO_MIME_TYPE), $_FILES[$input_name]['tmp_name'][$i]),
                            'file_ext' => pathinfo($_FILES[$input_name]['name'][$i], PATHINFO_EXTENSION),
                            'check_mime' => true,
                            'error_code' => $_FILES[$input_name]['error'][$i],
                        );
                        if ($count_mime_allowed > 0 && !in_array($file_info[$i]['mime'], $this->mime_allowed)) {
                            $file_error.='File type error';               
                            $file_info[$i]['check_mime'] = false;
                        }
                        if ($file_info[$i]['error_code'] === 0 && $file_info[$i]['check_mime'] === true) {
                            $save_path = $new_path .'/'. $file_name;
                            move_uploaded_file($_FILES[$input_name]['tmp_name'][$i], $save_path);
                            $file_info[$i]['file_path'] = $save_path;//存储路径
                            $file_info[$i]['web_path'] = $web_path .'/'. $file_name;
                            $file_flag = 1;
                        }else{
                            $file_info[$i]['file_path'] = '';
                            $file_info[$i]['web_path'] = '';
                        }
                    }
                }
                if ($file_flag) {
                    $ret = ['code'=>1,'msg'=>'success','data'=>$file_info];
                }else{
                    $ret = ['code'=>0,'msg'=>$file_error,'data'=>[]];
                }
            }else{
                $ret = ['code'=>0,'msg'=>'empty file','data'=>[]];
            }
        }else{
            $ret = ['code'=>0,'msg'=>'empty file','data'=>[]];
        }
        return $ret;
    }

}

你可能感兴趣的:(php文件上传)