简体   繁体   中英

Can't update image in yii project

I would like to upload an image and save the image name in database. I can create it and everything is ok, but update has problems. If i update the $model->img (image) value will be blank in db. This is my model rules:

            array('img', 'file','types'=>'jpg, gif, png, jpeg', 'allowEmpty'=>true, 'on'=>'update'),
            array('title, img', 'length', 'max'=>255, 'on'=>'insert,update'),

And this is the controller:

    public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
                $_SESSION['KCFINDER']['disabled'] = false;
                $_SESSION['KCFINDER']['uploadURL'] = Yii::app()->baseUrl."/../images/"; // URL for the uploads folder
                $_SESSION['KCFINDER']['uploadDir'] = Yii::app()->basePath."/../../images/"; // path to the uploads folder
        // $this->performAjaxValidation($model);
        if(isset($_POST['News']))
        {
                    $_POST['News']['img'] = $model->img;
                    $model->attributes=$_POST['News'];
                    $model->img = $_POST['News']['img'];
                        $uploadedFile=CUploadedFile::getInstance($model,'img');

            if($model->save()){
                            if(!empty($uploadedFile)) 
                            {
                                    $uploadedFile->saveAs(Yii::app()->basePath.'/../../images/'.$model->img);
                            }
                        }
                $this->redirect(array('view','id'=>$model->id));
        }

        $this->render('update',array(
            'model'=>$model,
        ));
    }

Before the save i check $model->img and it has value. So i think something wrong with save() in update.

$_POST['News']['img'] = $model->img; Why you did it? You must remove this line.

 $model->attributes=$_POST['News'];
 $model->img = $_POST['News']['img'];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM