Yii:使用CActiveForm获取表单文件的问题

My form:

<?php 
     $form=$this->beginWidget('CActiveForm', array(
    'id'=>'news-form',
    'htmlOptions'=>array('enctype'=>'multipart/form-data'),
));
?>

     <div class="form-group col-sm-12 col-md-6">
       <?php echo $form->labelEx($news,'state'); ?>
       <?php echo $form->dropDownList($news,'state', CHtml::listData($op,'Value','Type'),array('class' => 'form-control')) ?>
       <?php echo $form->error($news,'state'); ?>
      </div>

     <!-- HERE MY PROBLEM -->
     <input type="file" name="original" onchange="change(event)" id="original" />

     <?php echo CHtml::submitButton(Yii::t('app','CreateNewsLabel'), array('class' => 'btn btn-primary'), $news->isNewRecord ? 'Create' : 'Save'); ?>

When into my Controller I try to get file, that doesn't exists:

    if (isset($_FILE['original'])) {
        //..
    }

The if is never executed. I have tried a lot of possible solutions but doesn' work. I dont understand the reason because I'm using mulipart/form-data and should be exists...

Thanks you, regards.

dongsisui7562
dongsisui7562 哦,我的上帝...因为'S'而失去了2个小时...我很蠢D:..谢谢你Rob
2 年多之前 回复
dqkgxq0561
dqkgxq0561 你应该使用$_FILES而不是$_FILE-php.net/manual/en/reserved.variables.files.php
2 年多之前 回复

1个回答

It seems you have a typo. try to use

if (isset($_FILES['original'])) {
        //..
}

you can also try to do your validation like this via YII instead of doing the default PHP way.

$model->image=CUploadedFile::getInstance($model,'original');

In the view you can add this.

echo $form->labelEx($model, 'original');
echo $form->fileField($model, 'original');
echo $form->error($model, 'original');

to add validations to your file before uploading try this.

public function rules()
{
    return array(
        array('original', 'file', 'types'=>'jpg, gif, png', 'safe' => false),
    );
} 
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐