Issue
I want to update the uploaded file using yii2. I have the codes but nothing happens. I don't know whats wrong with my codes. Help me guys.
This is my form
<?= $form->field($model, 'doc_for')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'doc_from')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'drawer_id')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'doc_name')->textInput(['maxlength'=>true,'style'=>'width:500px']) ?>
<?= $form->field($model, 'doc_file')->fileInput() ?>
<div class="form-group">
<?= Html::submitButton($model->isNewRecord ? 'Create' : 'Update', ['class' => $model->isNewRecord ? 'btn btn-primary' : 'btn btn-primary']) ?>
</div>
Model
public function rules()
{
return [
[['reference_no', 'subject', 'doc_date', 'doc_for', 'drawer_id','doc_from','doc_name','doc_file'], 'required'],
[['reference_no'], 'integer'],
[['doc_date'], 'safe'],
[['subject', 'doc_for', 'drawer_id','doc_from','doc_name'], 'string', 'max' => 250],
[['doc_file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'docx, docs, doc',],
];
}
Controller create
public function actionCreate()
{
$model = new Documents();
if ($model->load(Yii::$app->request->post()))
{
$project =$model->doc_name;
$model->upload_file= UploadedFile::getInstance($model,'doc_file');
$model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
$model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
$model->save();
Yii::$app->getSession()->setFlash('success','Data saved!');
return $this->redirect(['view','id'=> $model->reference_no]);
}
else {
return $this ->render('create', [
'model'=>$model,
]);
}
}
Controller update
public function actionUpdate($id)
{
$model = new Documents();
$model = $this->findModel($id);
$current_image = $model->doc_file;
$project =$model->doc_name;
if ($model->load(Yii::$app->request->post())) {
$model->upload_file= UploadedFile::getInstance($model,'doc_file');
if(!empty($upload_file) && $upload_file->size !== 0) {
//print_R($image);die;
$model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
$model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
$model->upload_file = $current_image;
$model->save();
Yii::$app->getSession()->setFlash('success','Data updated!');
return $this->redirect(['view','id'=> $model->reference_no]);
}
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
I don't know whats wrong. Any ideas?
Solution
Ok thanks for the help. I got it. Now its working properly now. This is the code.
actionUpdate Controller:
public function actionUpdate($id)
{
$model = $this->findModel($id);
$current_image = $model->upload_file;
if ($model->load(Yii::$app->request->post())) {
$project =$model->doc_name;
$model->upload_file= UploadedFile::getInstance($model,'doc_file');
if(!empty($model->upload_file) && $model->upload_file->size !== 0) {
//print_R($image);die;
$model->upload_file->saveAs('uploads/'.$project.'.'.$model->upload_file->extension);
$model->doc_file='uploads/'.$project.'.'.$model->upload_file->extension;
}
else
$model->upload_file = $current_image;
$model->save();
Yii::$app->getSession()->setFlash('success','Data updated!');
return $this->redirect(['view', 'id' => $model->reference_no]);
} else {
return $this->render('update', [
'model' => $model,
]);
}
}
Answered By - user7978226
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.