From e82ffe8f989fb0be3b1b7fb84d3f2b864d83908e Mon Sep 17 00:00:00 2001 From: maxdancepro <31630905+maxdancepro@users.noreply.github.com> Date: Tue, 4 Jun 2019 03:17:36 +0300 Subject: [PATCH] =?UTF-8?q?=D0=A3=D0=B4=D0=B0=D0=BB=D0=B5=D0=BD=D0=B8?= =?UTF-8?q?=D0=B5=20=D1=84=D0=B0=D0=B9=D0=BB=D0=BE=D0=B2=20=D0=BF=D0=B5?= =?UTF-8?q?=D1=80=D0=B5=D0=B4=20=D0=B7=D0=B0=D0=B3=D1=83=D0=B7=D0=BA=D0=BE?= =?UTF-8?q?=D0=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/Behavior.php | 31 ++++++++++++------------------- 1 file changed, 12 insertions(+), 19 deletions(-) diff --git a/src/Behavior.php b/src/Behavior.php index 9c74496..495f416 100644 --- a/src/Behavior.php +++ b/src/Behavior.php @@ -156,10 +156,7 @@ public function beforeSave() if ($file = UploadedFile::getInstance($model, $attr)) { $this->createDirByAttr($attr); - - if (!$model->isNewRecord) { - $this->deleteFiles($attr); - } + $this->deleteFiles($attr); $fileName = $this->getFileName($file); @@ -243,25 +240,21 @@ private function getShortClassName($object) */ private function deleteFiles($attr) { - $base = $this->getSavePath($attr); - /* @var $model ActiveRecord */ - $model = $this->owner; - if ($model->isNewRecord) { - $value = $model->{$attr}; + $path = $this->getSavePath($attr); + + if (isset($this->deleteFileName)) { + $fileName = $this->deleteFileName; } else { - if (isset($this->deleteFileName)) { - $value = $this->deleteFileName; - } else { - $value = $model->oldAttributes[$attr]; - } + $fileName = $this->owner->model->{$attr}; } - $file = $base . $value; + + $file = $path . $fileName; $this->removeFile($file); if ($this->issetThumbnails($attr)) { - foreach ($this->attributes[$attr]['thumbnails'] as $name => $options) { - $this->ensureAttribute($name, $options); - $file = $base . $name . '_' . $value; + foreach ($this->attributes[$attr]['thumbnails'] as $tmb => $options) { + $this->ensureAttribute($tmb, $options); + $file = $path . $tmb . '_' . $fileName; $this->removeFile($file); } } @@ -491,7 +484,7 @@ private function createDirectory($dir): void */ private function removeFile($file): void { - if (is_file($file)) { + if (file_exists($file)) { unlink($file); } }