PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Thursday, January 20, 2022

[FIXED] update a record without removing the uploaded file in yii

 January 20, 2022     file, file-upload, php, yii     No comments   

Issue

I need to UPDATE record with out changing or deleting filename(i mean file) , OR reinsert file again so how i can do this?

here is my actionCreate, How i can write update?

public function actionCreate() {
    $model = new Page;
    if (isset($_POST['Page'])) {
        $model->attributes = $_POST['Page'];
        $model->filename = CUploadedFile::getInstance($model, 'filename');
        if ($model->save()) {
            if ($model->filename !== null) {
                $dest = Yii::getPathOfAlias('application.uploads');
                $model->filename->saveAs($dest . '/' . $model->filename->name);

                $model->save();
            }
            $this->redirect(array('view', 'id' => $model->id));
        }
    }
    $this->render('create', array(
        'model' => $model,
    ));
}

So Please can anyone find a solution


Solution

If you don't need to change or delete filename in edit/update then ignore filename (and upload part) assuming the file is alrready uploaded and you don't wish to change/delete it.

 public function actionUpdate($id) {
    $model = $this->loadModel($id);
    $file = $model->filename;
    if (isset($_POST['Page'])) {
        $model->attributes = $_POST['Page'];
        $model->filename = $file;
        if ($model->save()) {
            $this->redirect(array('view', 'id' => $model->id));
        }
    }
    $this->render('create', array(
        'model' => $model,
    ));
}


Answered By - yetanotherse
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing