yii-dream-team / yii2-upload-behavior

Yii2 file/image upload behavior for ActiveRecord
http://yiidreamteam.com/yii2/upload-behavior
MIT License
77 stars 45 forks source link

Please, add this to your good extension #24

Closed maxim-kn-akvilon closed 6 years ago

maxim-kn-akvilon commented 7 years ago

class CustomImageUploadBehavior extends ImageUploadBehavior {

/**
 * @var bool
 */
public $deleteTempFile = true;

/**
 * @throws Exception
 */
public function afterSave()
{
    if ($this->file instanceof UploadedFile) {
        $path = $this->getUploadedFilePath($this->attribute);
        FileHelper::createDirectory(pathinfo($path, PATHINFO_DIRNAME), 0775, true);
        if (!$this->file->saveAs($path, $this->deleteTempFile)) {
            throw new Exception('File saving error.');
        }
        $this->owner->trigger(static::EVENT_AFTER_FILE_SAVE);
    }
}

}

BioSin commented 6 years ago

Thank you for suggestions, this code FileUploadBehavior.php