在Yii中上传文件不显示图像名称/路径

时间:2014-11-08 20:42:17

标签: php yii

我再次遇到一个新问题。我正在尝试使用yii上传功能上传文件。

一切都能很好地保存,对图像有所帮助。这是我的代码:

控制器:

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,
        ));
    }

形式:

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

型号:

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'),
        );
    }

请帮助我开始工作。

1 个答案:

答案 0 :(得分:0)

首先添加enctype =&#34; multipart / form-data&#34;到您的表单标签或添加&#34; enctype&#34;如果您使用窗体小部件yo begin form,则为关联数组的选项。 如果它对您没有帮助,请在此处发布var_dump($_POST)个结果