Есть экшен:
Код: Выделить всё
public function actionProfile()
{
$model = Profile();
if ($model->load(\Yii::$app->request->post())) {
$model->egrFile = UploadedFile::getInstance($model, 'egrFile');
if ($model->uploadEgr() && $model->save()) {
\Yii::$app->getSession()->setFlash('success', \Yii::t('user', 'Your profile has been updated'));
return $this->refresh();
}
}
return $this->render('profile', [
'model' => $model,
]);
}
Код: Выделить всё
class Profile extends ActiveRecord
{
public $egrFile;
public $imageFile;
public static function tableName()
{
return '{{%profile}}';
}
public function uploadEgr()
{
if($this->validate()) {
if (!empty($this->egrFile)) {
@unlink(Yii::getAlias('@frontend/web/uploads/employer/egr/' . $this->file_egr));
$this->file_egr = time() . '_' . rand(1, 1000) . '.' . $this->egrFile->extension;
$this->egrFile->saveAs(Yii::getAlias('@frontend/web/uploads/employer/egr/' . $this->file_egr));
$this->egrFile = Yii::getAlias('@frontend/web/uploads/employer/egr/' . $this->file_egr);
}
return true;
}else {
return false;
}
}
/**
* @inheritdoc
*/
public function rules()
{
return [
['company_name', 'string', 'max' => 255],
['address', 'string', 'max' => 255],
['phone', 'string', 'max' => 55],
[['imageFile'], 'file', 'extensions' => 'gif, jpg, jpeg, png, txt', 'skipOnEmpty' => true],
[['egrFile'], 'file', 'skipOnEmpty' => true, 'extensions' => 'jpg, jpeg, gif, doc, docx, rtf, odt, pdf, txt'],
['body', 'string'],
];
}
}