update a record without removing the uploaded file

2019-08-30 06:07发布

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

2条回答
男人必须洒脱
2楼-- · 2019-08-30 06:38

Try this, it's working for me -

create a upload folder outside protected directory and also give read/write permission.

actionCreate function -

public function actionCreate() {

   $model = new Page();

    if(isset($_POST['Page']) && !empty($_POST['Page'])){

        $model->attributes = $_POST['Page'];

        $rand = rand(1, 999);
        $myfile = CUploadedFile::getInstance($model, 'filename');
        $fileName = "{$rand}-{$myfile}";  //Generate unique file name

           if(!empty( $fileName)){
               $model->filename   = $fileName;
               $target_dir = realpath( Yii::getPathOfAlias('application') . '/../upload');
               $target_file = $target_dir . '/' . $fileName;

               if(!$myfile->saveAs($target_file)){ 
                  $model->addError('filename', 'File saving error');
               }
            }

        if(!$model->hasErrors() && $model->validate() && $model->save(false)) {
         $this->redirect(array('view', 'id'=>$model->id, 'msg'=>'success'));
       }
    }
}

actionUpdate function -

public function actionUpdate() {

  $model = Page::model()->findbyPK($id);
  $model->scenario = 'update';

  if(isset($_POST['Page']) && !empty($_POST['Page'])){
     $model->attributes = $_POST['Page'];

     if(empty($_POST['Page']['filename'])) { $file_name = $model->filename; } //store existing file name into a variable if your uploading a file on update

     $rand = rand(1, 999);
     $myfile = CUploadedFile::getInstance($model, 'filename');
     $fileName = "{$rand}-{$myfile}";  //Generate unique file name

     if($model->validate()) {
       if(!empty($fileName)) {
           $model->filename = $fileName;
           $target_dir  = realpath( Yii::getPathOfAlias( 'application' ) . '/../upload');
           $target_file  = $target_dir . '/' . $fileName;

           if(!$myfile->saveAs($target_file)){ 
               $model->addError('filename', 'File saving error');
            }
        } 
      else {
             $model->filename = $file_name; //allow existing file name 
           }

     if( !$model->hasErrors() && $model->save(false) ) {
            $this->redirect(array('view', 'id'=>$model->id, 'msg'=>'update'));
          }
       }
  }

In model rules array define this -

array('filename', 'file', 'types'=>'jpg, jpeg, bmp, gif, png', 'allowEmpty'=>true, 'on'=>'insert, update')
查看更多
在下西门庆
3楼-- · 2019-08-30 06:44

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,
    ));
}
查看更多
登录 后发表回答