上传使用FileInput插件
官方文档 http://demos.krajee.com/widge...
也参考了 http://www.manks.top/yii2_mul...
插件参数比较多。前台表单部分,能够参照具体例子写就好了。php
主要是上传以后的处理和验证,验证文件的类型,大小等,我写了一个公共方法来处理上传html
/** * 文件上传 * ``` * $model = new UploadValidate($config_name); * $result = CommonHelper::myUpload($model, $field, 'invoice'); * ``` * * @param object $model \common\models\UploadValidate 验证上传文件 * @param string $field 上传字段名称 * @param string $path 文件保存路径 * * @return bool|array */ public static function myUpload($model, $field, $path = '') { $upload_path = \Yii::$app->params['upload_path']; $path = $path ? $path . "/" : ''; if (\Yii::$app->request->isPost) { $file = UploadedFile::getInstanceByName($field); $model->file = $file; //文件上传存放的目录 $dir = $upload_path . $path . date("Ymd"); if ( !is_dir($dir)) { mkdir($dir, 0777, true); chmod($dir, 0777); } if ($model->validate()) { //生成文件名 $rand_name = rand(1000, 9999); $fileName = date("YmdHis") . $rand_name . '_' . $model->file->baseName . "." . $model->file->extension; $save_dir = $dir . "/" . $fileName; $model->file->saveAs($save_dir); $uploadSuccessPath = $path . date("Ymd") . "/" . $fileName; $result['file_name'] = $model->file->baseName; $result['file_path'] = $uploadSuccessPath; } else { //上传失败记录日志 self::recordLog($model->errors, $field, 'Upload'); return false; } } else { return false; }
验证的model类,参数没有写那么多,能够把须要的参数都加上,每一个项目或者模块须要用到上传的时候,在配置文件params里配置上相关的参数就能够了yii2
<?php namespace common\models; use yii\base\Model; /** * Class UploadValidate 文件上传验证 * 使用model验证文件上传字段 * ``` * $model = new UploadValidate($config_name); * ``` * * @package common\models * @author windhoney * @package common\models */ class UploadValidate extends Model { /** * @var string 表单字段名 */ public $file; /** * @var array|string 扩展名 */ public $extensions; /** * @var int 文件大小 最大值 单位字节 */ public $max_size = 60 * 1024 * 1024; /** * @var int 文件大小 最小值 单位字节 */ public $min_size = 1; /** * @var array|string MIME TYPE */ public $mime_type; /** * @var string 上传失败后返回信息 */ public $message = '上传失败'; /** * UploadValidate constructor. * * @param string $config_name `@app/config/params.php` 文件上传验证配置项名称 */ public function __construct($config_name) { parent::__construct(); $upload_config = \Yii::$app->params[$config_name]; $this->extensions = $upload_config['extensions']??''; $this->mime_type = $upload_config['mime_types']??''; $this->max_size = $upload_config['max_size']??''; $this->min_size = $upload_config['min_size']??''; $this->message = $upload_config['message']??''; } /** * @inheritdoc 验证规则 */ public function rules() { $file_rule = [['file'], 'file']; if ($this->extensions) { $file_rule['extensions'] = $this->extensions; } if ($this->mime_type) { $file_rule['mimeTypes'] = $this->mime_type; } if ($this->max_size) { $file_rule['maxSize'] = $this->max_size; } if ($this->min_size) { $file_rule['minSize'] = $this->min_size; } if ($this->message) { $file_rule['message'] = $this->message; } $rules = [$file_rule]; return $rules; } }
配置文件 @app/config/params.phpapp
<?php return [ 'test_upload' => [ 'extensions' => ['jpg', 'png', 'jpeg', 'jpe', 'pdf'], 'mime_types' => ['image/*', 'application/pdf'], 'max_size' => 10 * 1024 * 1024, 'min_size' => 1, 'message' => '上传失败', ] ];
使用步骤yii
//实例化上传验证类,传入上传配置参数项名称 $model = new UploadValidate('test_upload'); //上传 $result = CommonHelper::myUpload($model, $field, 'test');