Answer the question
In order to leave comments, you need to log in
How to save image in yii2?
I read the manual, watched the video, still does not save. Tell me who knows.
View
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?>
<?= $form->field($model, 'image')->fileInput() ?>
...
controller
public function actionUpdate($id)
{
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post())) {
$model->image = UploadedFile::getInstance($model, 'image');
if( $model->image ){
$model->upload();
}
Yii::$app->session->setFlash('success',
return $this->redirect(['view', 'id' => $model->id]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
model
public function upload(){
if($this->validate()){
$path = 'upload/store/' . $this->image->baseName . '.' . $this->image->extension;
$this->image->saveAs($path);
$this->attachImage($path, true);
@unlink($path);
return true;
}else{
return false;
}
}
Answer the question
In order to leave comments, you need to log in
Good afternoon.
First check the write permissions of the directory.
The second point is to check the path to the directory. Where is your download directory?
Suppose, if in the views directory, then the path may be similar to this
. In order not to write a long path, you can make an alias
and use this alias already
$this->path = Yii::getAlias('@checktext');
//....
$this->fileUpload->saveAs($this->path . '/' . $this->filename);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question