Answer the question
In order to leave comments, you need to log in
Yii2, how to load image (user avatar) in default User model?
The problem is the following:
1) When I attach an image, only the name of the image is written to the database, without the path, although I specify it.
2) UploadedFile in the controller does not work for some reason, images are not loaded. added structure:
if(!UploadedFile::getInstance($model,'avatar')){echo 'error!';}
use yii\web\UploadedFile;
public function actionUpdateuserinfo($id)
{
$model = User::find()->where('id = :id',[':id' => $id])->one();
if( \Yii::$app->getRequest()->isPost ){
if( $model->load( \Yii::$app->request->post() ) && $model->validate() ){
$model->avatar = UploadedFile::getInstance($model,'avatar');
if($model->avatar){
$dir = 'images/user_avatars/';
$path = $model->avatar->baseName.'.'.$model->avatar->extention;
$model->avatar->saveAs($dir.$path);
$model->attachImage($path);
}
$model->save();
$user = User::find(
['username','email','first_name','surname','date_of_birth','avatar']
)->where(['id' => $model->id])->all();
return $this->render('index', [
'user' => $user,
'model' => $model
]);
}
}
else{
return $this->render('updateuserinfo', [
'model' => $model
]);
}
}
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\widgets\Pjax;
?>
<?php Pjax::begin(['id' => 'w0']); ?>
<?php
$form = ActiveForm::begin([
'id' => 'about-form',
'method' => 'post',
'options' => [
'onctype' => 'multipart/form-data',
],
]); ?>
<?= $form->field($model, 'avatar')->fileInput() ?>
<?= $form->field($model, 'first_name')->textInput(['placeholder' => 'Ваше имя', 'value' => $model->first_name]) ?>
<?= $form->field($model, 'email')->textInput(['placeholder' => 'Ваш Email', 'value' => $model->email]) ?>
<?= $form->field($model, 'surname')->textInput(['placeholder' => 'Ваша фамилия', 'value' => $model->surname]) ?>
<?= $form->field($model, 'date_of_birth') ?>
<div class="form-group">
<?= Html::submitButton('Отправить', ['class' => 'btn btn-primary']) ?>
</div>
<?php ActiveForm::end(); ?>
<?php Pjax::end(); ?>
use yii\web\UploadedFile;
public function rules()
{
return [
['status', 'default', 'value' => self::STATUS_ACTIVE],
['status', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],
,
];
}
Answer the question
In order to leave comments, you need to log in
In general, the problem was solved using a third-party plugin for loading images using ajax, here is the actual link to the plugin, there is also a live demo and a link to the project on github. I did the work like this: there is a , using the plugin, I validate and upload the image to the server, upon successful upload, js enters the path to the uploaded image into the input, and then everything using standard Yii2 tools, the pjax-powered form is sent, and the data is validated, including the address images are written to the database, profit! Who will use this method, keep in mind that when uploading several images with the same name, they will replace each other. Therefore, it is necessary to hash the names before loading and, accordingly, write the already updated name to the database. In my case, before hashing, the user's nickname is added to the image name. it is unique, then the name of the image is generated unique.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question