I upload photos in profile editing. For the field of loading the field has registered
<?= $form->field($model, 'image')->fileInput(); ?> When I submit a form, I conclude what is being transmitted.
print_r(Yii::$app->request->post()); and get it
Array ( [_csrf] => SzZPazVRNHUbRx80fSFXAAEBGz1mB1gNOG96HVwVWTBmQX8denx.Ig== [EditProfile] => Array ( [username] => zaych [email] => mail@site.ru [first_name] => Admin [second_name] => Admin [image] => ) [signup-button] => ) That is, the image field is empty, although I choose a photo. For the form prescribed
'enctype' => 'multipart/form-data' I do the same
$file->image = UploadedFile::getInstance($file, 'image'); print_r($file->image); And also empty What could be the error?
Adding code: 1) Submission
<?php $form = ActiveForm::begin([ 'id' => 'edit-profile', 'options' => ['class' => 'form-horizontal', 'enctype' => 'multipart/form-data'], 'fieldConfig' => [ 'options' => ['class' => 'input-group'], 'inputOptions' => ['class' => 'form-control'] ], ]); ?> <div class="form-group"> <?= $form->field($model, 'username')->textInput(['value'=>$info['username']]); ?> </div> <div class="form-group"> <?= $form->field($model, 'email')->textInput(['value'=>$info['email']]); ?> </div> <div class="form-group"> <?= $form->field($model, 'first_name')->textInput(['value'=>$info['first_name']]); ?> </div> <div class="form-group"> <?= $form->field($model, 'second_name')->textInput(['value'=>$info['second_name']]); ?> </div> <div class="form-group"> <?= $form->field($model, 'image')->fileInput(); ?> </div> <div class="form-group"> <?= Html::submitButton('Зарегистрироваться', ['class' => 'btn btn-primary', 'name' => 'signup-button']) ?> </div> 2) Model
namespace frontend\models\profile; use yii\base\Model; use yii\web\UploadedFile; class UploadForm extends Model { /** * @var UploadedFile */ public $image; public function rules() { return [ [['image'], 'file', 'extensions' => 'png, jpg, gif, bmp'], ]; } public function upload() { if ($this->validate()) { print_r($this->image);exit; $this->image->saveAs('/source/user/' . $this->image->baseName . '.' . $this->image->extension); return $this->image->baseName . '.' . $this->image->extension; } else { return 'error'; } } } 3) Controller
public function actionProfileEdit(){ $user=new User(); $model=new EditProfile(); $file=new UploadForm(); if($model->load(Yii::$app->request->post())){ $file->image = UploadedFile::getInstance($file, 'image'); print_r($file->image);exit; $res=$file->upload(); if ($model->updateProfile()){ ?> <script>msg('Профиль успешно изменен', 'success');</script> <?php } else { ?> <script>msg('Произошла ошибка', 'error');</script> <?php } } $params = [ 'sid' => Yii::$app->user->identity->sid, 'role' => $user->getUserType(), 'info' => $user->getUserInfo(), 'uid' => Yii::$app->user->getId(), 'model' => $model ]; return $this->render('profile/profile-edit', $params); }