A
A
agent11562016-09-11 15:07:28
Yii
agent1156, 2016-09-11 15:07:28

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

2 answer(s)
D
Dmitry, 2016-09-11
@slo_nik

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);

ps Are there any validation rules in the UploadForm model?

A
agent1156, 2016-09-11
@agent1156

Can write permissions be closed on the local Windows server too?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question