Answer the question
In order to leave comments, you need to log in
Yii2 how to make image update?
Good afternoon,
I want to update the record of pictures, I use use kartik\file\FileInput;
Here is the action code itself
$dir = Yii::getAlias('@app/web/uploads/');
$uploaded = false;
$model = new Programs();
if($_POST){
if (Yii::$app->request->isAjax) {
Yii::$app->response->format = Response::FORMAT_JSON;
return ActiveForm::validate($model);
}else{
$model->load(Yii::$app->request->post('Programs'));
$programs = Yii::$app->request->post('Programs');
$guid = UploadedFile::getInstance($model, 'guid');
$extension = strtolower(substr(strrchr($guid->name, '.'), 1));
$filename = DFileHelper::getRandomFileName($dir, $extension);
$model->title = $programs['title'];
$model->description = $programs['description'];
$model->text = isset($programs['text']) ? $programs['text'] : '';
$model->time_show = isset($programs['time_show']) ? $programs['time_show'] : '';
if ($model->validate()) {
$uploaded = $guid->saveAs( $dir . $filename.'.'. $extension );
$model->guid = EasyThumbnailImage::thumbnailFile($dir . $filename.'.'. $extension, 341, 227);
$model->flag = 1;
if ($model->save()){
Yii::$app->response->redirect(array('blog/read', 'id' => $model->id));
}
}
}
}
$guid = UploadedFile::getInstance($model, 'guid');
Answer the question
In order to leave comments, you need to log in
show the form generation code. sure you didn't set
<?php $form = ActiveForm::begin([
'options' => ['enctype'=>'multipart/form-data']
]); ?>
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question