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); } 
  • show controller action, model, view - Bookin
  • added a question - Diefair

1 answer 1

I think it is clear that in the $_POST array (in this case, the method that returns it Yii::$app->request->post() ) you will not find the file. Files in the $_FILES array $_FILES .

In addition to input[type="file"] Yii also adds input[type="hidden"] with the name image , so you see an empty field in the post .

The problem you have is that you send the file with the name addressed to the EditProfile model, and look for it in the UploadForm model.

The UploadedFile::getInstance() method does not just request a model; it forms the name of the model class and searches for the name in the $_FILES .

As a result, your code UploadedFile::getInstance($file, 'image') searches for a file in $_FILES[UploadForm][image] and you have it, because of - $form->field($model, 'image')->fileInput() , comes in $_FILES[EditProfile][image] .

Or pass an instance of the UploadForm class to the view and change the form field:

$form->field($file, 'image')->fileInput()

Or change UploadedFile::getInstance() to UploadedFile::getInstance($model, 'image') . Nothing prevents you from transferring the attribute, the rule, and the loading method to the EditProfile model, if there is no goal to make a unique model for loading files.

  • Thanks a lot, you really helped. I only understand yii2, so I make such mistakes. Can you recommend a good book on yii2? - Diefair
  • I do not know the books, their guide is completely good - yiiframework.com/doc-2.0/guide-index.html (there is a translation), and documentation. When problems arise, it is worth exploring the source code, and everything falls into place, plus experience. - Bookin
  • thanks for the help - Diefair