public function upload() { if ($this->validate()) { if (isset($this->avatarFile)) { if (!is_dir(Yii::$app->params['uploadPath'] . '/avatar')) { mkdir(Yii::$app->params['uploadPath'] . '/avatar', 0777, true); } $this->avatarFile->saveAs(Yii::$app->params['uploadPath'] . '/avatar/' . $this->avatarFile->baseName . '.' . $this->avatarFile->extension); $this->avatar = '/avatar/' . $this->avatarFile->baseName . '.' . $this->avatarFile->extension; $this->save(false); } return true; } else { return false; } }
public function upload() { $this->setScenario('upload'); if ($this->validate()) { $patch = Yii::getAlias('@uploads/' . $this->dirName); if (!is_dir($patch)) { mkdir($patch); } $fileName = 'sl_' . Yii::$app->security->generateRandomString(8) . '.' . $this->imageFile->extension; if ($this->imageFile->saveAs($patch . '/' . $fileName)) { $this->removeImage(); } $this->image = $fileName; return true; } return false; }
/** * Save the uploaded file to filesystem path and optional resize before save it. */ protected function saveUploadedFile() { $uniqueId = uniqid(mt_rand(100, 1000)); $newName = $this->mediaFile->baseName . '_' . $uniqueId . '.' . $this->mediaFile->extension; $webroot = Yii::getAlias('@webroot'); $dir = $webroot . DIRECTORY_SEPARATOR . $this->targetUrl; FileHelper::createDirectory($dir); $path = $dir . DIRECTORY_SEPARATOR . $newName; $this->name = $this->mediaFile->name; $this->url = $this->targetUrl . '/' . $newName; $this->type = $this->mediaFile->type; // resize the file (Check is done by [[addMediaRules]] if (is_array($this->resize)) { list($width, $height) = $this->resize; $image = \kmergen\media\helpers\Image::thumb($this->mediaFile->tempName, $width, $height)->save($path); $this->size = filesize($path); } else { $this->size = $this->mediaFile->size; $this->mediaFile->saveAs($path); } }