Skip to content

Commit ecbfb0c

Browse files
committed
fix:backend upload admin user avatar and artice thumb only permit to png/jpg/jpeg/gif/webp
1 parent cf82945 commit ecbfb0c

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

backend/models/User.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use common\helpers\Util;
1414
use yii\base\Event;
1515
use yii\web\ForbiddenHttpException;
16+
use yii\web\UploadedFile;
1617

1718
/**
1819
* User model
@@ -23,6 +24,7 @@
2324
* @property string $password_reset_token
2425
* @property string $email
2526
* @property string $auth_key
27+
* @property string $avatar
2628
* @property integer $status
2729
* @property integer $created_at
2830
* @property integer $updated_at
@@ -96,6 +98,14 @@ public function attributeLabels()
9698
];
9799
}
98100

101+
public function beforeValidate()
102+
{
103+
if($this->avatar !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
104+
$this->avatar = UploadedFile::getInstance($this, "avatar");
105+
}
106+
return parent::beforeValidate();
107+
}
108+
99109
public function beforeSave($insert)
100110
{
101111
Util::handleModelSingleFileUpload($this, 'avatar', $insert, '@admin/uploads/avatar/');

common/models/Article.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Yii;
1616
use common\libs\Constants;
1717
use yii\behaviors\TimestampBehavior;
18+
use yii\web\UploadedFile;
1819

1920
/**
2021
* This is the model class for table "{{%article}}".
@@ -109,7 +110,6 @@ public function rules()
109110
'title',
110111
'sub_title',
111112
'summary',
112-
'thumb',
113113
'seo_title',
114114
'seo_keywords',
115115
'seo_description',
@@ -295,6 +295,14 @@ public function afterFind()
295295
parent::afterFind();
296296
}
297297

298+
public function beforeValidate()
299+
{
300+
if ($this->thumb !== "0") {//为0表示需要删除图片,Util::handleModelSingleFileUpload()会有判断删除图片
301+
$this->thumb = UploadedFile::getInstance($this, "thumb");
302+
}
303+
return parent::beforeValidate();
304+
}
305+
298306
public function beforeSave($insert)
299307
{
300308
if ($this->thumb) {

0 commit comments

Comments
 (0)