Zhomart  Kassymov
Zhomart Kassymov

Reputation: 3

update the uploaded file yii2

I have used following to upload image and save it in server and database. Now I want to write a controller to update the uploaded image. how to do it???

controller

public function actionInvitfile()
{
    $model = new Applicants();
    $imgName = Yii::$app->user->identity->id;

        if($model->load(Yii::$app->request->post())){
        $model->file = UploadedFile::getInstance($model, 'file');
        $model->file->saveAs('uploads/invitfile/' . $imgName . '.' . $model->file->extension);
        $model->invitations_file='uploads/invitfile/'. $imgName . '.' . $model->file->extension;
        $model->save(false);
        }
   return $this->goHome();
}

Model

    class Applicants extends \yii\db\ActiveRecord
{
    public $file;
    public static function tableName()
    {
        return 'applicants';
    }

    public function rules()
    {
        return [
            [['file'], 'file', 'skipOnEmpty' => true, 'extensions' => 'pdf'],
        ];
    }

Please, Help me!)

Upvotes: 0

Views: 1222

Answers (2)

Awais Mustafa
Awais Mustafa

Reputation: 151

If you want to update a single image, then in update function, before loading post variables, just keep the old image in a variable. The following code will help you:

public function actionUpdate($id)
{
    $model = $this->findModel($id);
    $oldImage = $model->banner;

    if ($model->load(Yii::$app->request->post())) {

        //get picture data and save it
        $imageFile = \yii\web\UploadedFile::getInstance($model, 'banner');
        if($imageFile) {
            unlink(Yii::getAlias('@app').'/../../uploads/banners/' . $oldImage);

            $fileName = $imageFile->baseName.'_'.time().'.'.$imageFile->extension;
            $imageFile->saveAs(Yii::getAlias('@app').'/../../uploads/banners/' . $fileName);

            $model->banner = $fileName;
            $model->save();
        } else {
            $model->banner = $oldImage;
            $model->save(false);

        }

        return $this->redirect(['index']);
    } else {
        return $this->render('update', [
            'model' => $model,
        ]);
    }
}

Upvotes: 0

eborrallo
eborrallo

Reputation: 750

I think this can work

public function actionUpdate($id)
    {
         $imgName = Yii::$app->user->identity->id;
         $model = Applicants->findModel($id);
        if($model->load(Yii::$app->request->post())){

         unlink($model->invitations_file); 


         $model->file = UploadedFile::getInstance($model, 'file');
         $model->file->saveAs('uploads/invitfile/' . $imgName . '.' . $model->file->extension);
         $model->invitations_file='uploads/invitfile/'. $imgName . '.' . $model->file->extension;
         $model->save(false);
        }
   return $this->goHome();

    }

but here you have official documentation of the kartij blog where you can learn much more and have a better answer to your problem:
http://webtips.krajee.com/advanced-upload-using-yii2-fileinput-widget/

Upvotes: 1

Related Questions