Skip to content

Commit

Permalink
fix:backend upload admin user avatar and artice thumb only permit to …
Browse files Browse the repository at this point in the history
…png/jpg/jpeg/gif/webp
  • Loading branch information
liufee committed Dec 24, 2019
1 parent cf82945 commit ecbfb0c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
10 changes: 10 additions & 0 deletions backend/models/User.php
Expand Up @@ -13,6 +13,7 @@
use common\helpers\Util;
use yii\base\Event;
use yii\web\ForbiddenHttpException;
use yii\web\UploadedFile;

/**
* User model
Expand All @@ -23,6 +24,7 @@
* @property string $password_reset_token
* @property string $email
* @property string $auth_key
* @property string $avatar
* @property integer $status
* @property integer $created_at
* @property integer $updated_at
Expand Down Expand Up @@ -96,6 +98,14 @@ public function attributeLabels()
];
}

public function beforeValidate()
{
if($this->avatar !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
$this->avatar = UploadedFile::getInstance($this, "avatar");
}
return parent::beforeValidate();
}

public function beforeSave($insert)
{
Util::handleModelSingleFileUpload($this, 'avatar', $insert, '@admin/uploads/avatar/');
Expand Down
10 changes: 9 additions & 1 deletion common/models/Article.php
Expand Up @@ -15,6 +15,7 @@
use Yii;
use common\libs\Constants;
use yii\behaviors\TimestampBehavior;
use yii\web\UploadedFile;

/**
* This is the model class for table "{{%article}}".
Expand Down Expand Up @@ -109,7 +110,6 @@ public function rules()
'title',
'sub_title',
'summary',
'thumb',
'seo_title',
'seo_keywords',
'seo_description',
Expand Down Expand Up @@ -295,6 +295,14 @@ public function afterFind()
parent::afterFind();
}

public function beforeValidate()
{
if ($this->thumb !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
$this->thumb = UploadedFile::getInstance($this, "thumb");
}
return parent::beforeValidate();
}

public function beforeSave($insert)
{
if ($this->thumb) {
Expand Down

0 comments on commit ecbfb0c

Please sign in to comment.