Skip to content

Commit 6d3a7d3

Browse files
committed
Fixed if no file select
1 parent ca41727 commit 6d3a7d3

File tree

1 file changed

+31
-12
lines changed

1 file changed

+31
-12
lines changed

UploadFileBehavior.php

Lines changed: 31 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,21 @@
88
use yii\web\UploadedFile;
99
use Yii;
1010

11+
/**
12+
* Class UploadFileBehavior
13+
* @package yii\behaviors
14+
*
15+
* @property UploadedFile|null $uploadFile
16+
*/
1117
class UploadFileBehavior extends Behavior
1218
{
19+
/**
20+
* @var ActiveRecord
21+
*/
22+
public $owner;
23+
/**
24+
* @var string
25+
*/
1326
public $attribute = '';
1427

1528
/**
@@ -21,9 +34,6 @@ class UploadFileBehavior extends Behavior
2134
* @var string The unique file name;
2235
*/
2336
private $_fileName;
24-
/**
25-
* @var null|UploadedFile Get instance class UploadFile
26-
*/
2737
private $_uploadFile;
2838

2939
public function events()
@@ -38,6 +48,7 @@ public function events()
3848

3949
public function init()
4050
{
51+
parent::init();
4152
if ($this->uploadDir instanceof \Closure) {
4253
$this->uploadDir = call_user_func($this->uploadDir);
4354
} else {
@@ -48,7 +59,9 @@ public function init()
4859
public function beforeValidate()
4960
{
5061
$this->setUploadFile();
51-
$this->owner->setAttribute($this->attribute, $this->getUploadFile());
62+
if ($this->uploadFile && $this->uploadFile instanceof UploadedFile) {
63+
$this->owner->setAttribute($this->attribute, $this->uploadFile);
64+
}
5265
}
5366

5467
public function beforeInsert()
@@ -68,31 +81,37 @@ public function beforeDelete()
6881
$this->deleteFile();
6982
}
7083

84+
/**
85+
* @return bool
86+
* @throws \yii\base\Exception
87+
*/
7188
protected function saveFile()
7289
{
73-
if ($this->getUploadFile() instanceof UploadedFile) {
90+
if ($this->uploadFile && $this->uploadFile instanceof UploadedFile) {
7491
if (!file_exists($this->uploadDir)) {
75-
FileHelper::createDirectory($this->uploadDir);
92+
FileHelper::createDirectory($this->uploadDir, '0777');
7693
}
77-
if ($this->getUploadFile()->saveAs($this->getFilePath($this->getFileName()))) {
94+
if ($this->uploadFile->saveAs($this->getFilePath($this->getFileName()))) {
7895
$this->owner->setAttribute($this->attribute, $this->getFileName());
7996
return true;
8097
}
98+
} else {
99+
$this->owner->setAttribute($this->attribute, $this->owner->getOldAttribute($this->attribute));
81100
}
82101
return false;
83102
}
84103

85104
protected function deleteFile()
86105
{
87-
if (!$this->owner->getOldAttribute($this->attribute) && file_exists($this->getFilePath($this->owner->getOldAttribute($this->attribute)))) {
88-
@unlink($this->getFilePath($this->owner->getOldAttribute($this->attribute)));
106+
if ($this->owner->getOldAttribute($this->attribute) && file_exists($this->getFilePath($this->owner->getOldAttribute($this->attribute)))) {
107+
unlink($this->getFilePath($this->owner->getOldAttribute($this->attribute)));
89108
}
90109
}
91110

92111
public function getFileName()
93112
{
94113
if (!$this->_fileName) {
95-
$this->_fileName = Inflector::slug($this->getUploadFile()->baseName) . strtolower(Yii::$app->security->generateRandomString(13)) . '-' . '.' . $this->getUploadFile()->extension;
114+
$this->_fileName = Inflector::slug($this->uploadFile->baseName) . '-' . strtolower(Yii::$app->security->generateRandomString(13)) . '.' . $this->uploadFile->extension;
96115
}
97116
return $this->_fileName;
98117
}
@@ -107,7 +126,7 @@ public function getFilePath($fileName)
107126
}
108127

109128
/**
110-
* Get Instance UploadFile
129+
* Get Instance UploadFile if
111130
*/
112131
public function setUploadFile()
113132
{
@@ -117,7 +136,7 @@ public function setUploadFile()
117136
}
118137

119138
/**
120-
* @return null|UploadedFile
139+
* @return UploadedFile|null
121140
*/
122141
public function getUploadFile()
123142
{

0 commit comments

Comments
 (0)