简体   繁体   中英

upload file in Yii not showing image name/path

im here again with a new problem. I am trying to upload a file using yii upload function.

Everything saves well, exept for the image. Here's my code:

Controller:

public function actionUpdate($id)
    {
        $model=$this->loadModel($id);
        $dir = Yii::getPathOfAlias('webroot.images.producten');
        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if(isset($_POST['Producten']))
        {
            $model->attributes=$_POST['Producten'];
            $model->images=CUploadedFile::getInstance($model,'images');
            $nf = $model->images;

            if($model->save()){
                $this->redirect(array('view','id'=>$model->id));
                $model->images->saveAs($dir.'/'.$nf);
                $model->images = $nf;
                $model->save();
            }
        }

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

Form:

<div class="row">
    <?php echo $form->labelEx($model,'images'); ?>
    <?php echo CHtml::activeFileField($model,'images'); ?>
    <?php echo $form->error($model,'images'); ?>
</div>

Model:

public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('naam, beschrijving, prijs', 'required'),
            array('images,', 'file', 'allowEmpty'=>true,
                'safe' => true,
                'types'=> 'jpg, jpeg, png, gif',
                'maxSize' => (1024 * 300), // 300 Kb
            ),
            array('aangepast_door', 'numerical', 'integerOnly'=>true),
            array('naam', 'length', 'max'=>50),
            array('prijs, actieprijs', 'length', 'max'=>10),
            //array('toegevoegd, aangepast', 'safe'),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('naam, beschrijving, prijs, actieprijs', 'safe', 'on'=>'search'),
        );
    }

Please help me get this to work.

First of all add enctype= "multipart/form-data" to your form tag or add "enctype" option to associative array if you used form widget yo begin form. If it will not helps you, please post var_dump($_POST) results here

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