Yii和自定义文件上传(使用jasny bootstrap)

时间:2013-02-20 12:09:42

标签: twitter-bootstrap file-upload yii jasny-bootstrap

由于各种原因,我不能使用正常的方式来构建表单,所以我略微改变了一些位。 对于正常的帖子操作,这很有效,但是当我尝试上传文件时,一切都会出错。

我把这个作为表单,上传了Jasny文件。

<?php $form=$this->beginWidget('CActiveForm', array(
    'id'=>'plan_style_form_'.$data->id,
    'enableAjaxValidation'=>false,
    'htmlOptions' => array(
        'enctype' => 'multipart/form-data',
    ),

)); ?>

........
                        <div class="fileupload fileupload-exists" data-provides="fileupload" data-name="RssPlanStyle[tab_icon]">
                            <div class="fileupload-new thumbnail" style="width: 111px; height: 74px;"><img src="http://www.placehold.it/200x150/EFEFEF/AAAAAA&text=no+image" /></div>
                            <div class="fileupload-preview fileupload-exists thumbnail" style="width: 111px; height: 74px;"><img src="../images/tab_images/<?php echo $data->RssPlanStyle->tab_icon ?>"></div>
                                <div class="plan_tab_custom_title plan_tab_example_title_<?php echo Chtml::encode($data->RssPlanStyle->id); ?>">
                                    <?php echo Chtml::encode($data->RssPlanStyle->titel_tab); ?>
                                </div><!-- .plan_tab_custom_title -->
                        <div>
                        <div class="span6">
                        <label>Tab icoon</label>
                        <span class="btn btn-file"><span class="fileupload-new">Selecteer Afbeelding</span><span class="fileupload-exists">Verander Afbeelding</span><input type="file" /></span>
                        <a href="#" class="btn fileupload-exists" data-dismiss="fileupload">Verwijder Afbeelding</a>
                        </div></div>
.......
                <?php echo CHtml::ajaxSubmitButton(
                    'Opslaan',
                    array('rssPlanStyle/updateStyle'),
                    array('update'=>'#result-style-'.$data['id']),
                    array('class'=>'btn btn-success', 'style'=>'float:right; margin-bottom:20px;')
                ); ?>
.........
<?php $this->endWidget(); ?>

如上所述,所有的帖子数据都运行正常,但是当我来到这个文件输入时,我确实得到了$_POST['RssPlanStyle']['tab_icon'],但$_FILES['RssPlanStyle']['tab_icon']是空的。 这是我的上传功能atm。

public function actionUpdateStyle(){

// tab_icon
if(isset($_POST['RssPlanStyle']['tab_icon'])) {
// Als afbeelding is veranderd
    if ($_POST['RssPlanStyle']['tab_icon'] == '') {
    // Als afbeelding is verwijderd
        echo 'image verwijderd';
    } else {
    // Als er een nieuwe afbeelding is
        if(empty($_FILES['RssPlanStyle']['tab_icon'])){
            echo 'leeg';
        }

        echo 'new image';

    }
} else {
// Als afbeelding niet is veranderd
    echo "niks veranderd";
}

}

所以我进入了echo 'new image'部分,但也显示了echo 'leeg'。我做错了什么?

处理上传的正确方法是什么。

1 个答案:

答案 0 :(得分:0)

    if (yii::app()->request->isPostRequest) {

            $model = new ModelForm;
            $model->file=CUploadedFile::getInstance($model,'RssPlanStyle');

            if ($model->file != NULL){
                echo 'new image';
            }else{
                echo 'no image';
            }
    }

您可以使用getInstances()来处理多重上传。请参阅http://www.yiiframework.com/doc/api/1.1/CUploadedFile#getInstance-detail