【问题标题】:upload file in Yii not showing image name/path在 Yii 中上传文件不显示图像名称/路径
【发布时间】:2014-11-08 20:42:17
【问题描述】:

我又来了一个新问题。我正在尝试使用 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'),
        );
    }

请帮我解决这个问题。

【问题讨论】:

    标签: php yii


    【解决方案1】:

    首先将 enctype="multipart/form-data" 添加到您的表单标签或添加“enctype”选项到关联数组,如果您使用表单小部件哟开始表单。 如果对您没有帮助,请在此处发布var_dump($_POST) 结果

    【讨论】:

    • 在关联数组中添加“enctype”选项是什么意思?
    • @Deer-Outdoor.nl ,类似的东西:&lt;?php $form=$this-&gt;beginWidget('CActiveForm', array( 'id'=&gt;'miscellaneous-pages-form', 'enableAjaxValidation'=&gt;false, 'htmlOptions'=
    猜你喜欢
    • 2015-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-04
    • 2016-06-21
    • 2023-03-25
    相关资源
    最近更新 更多