Yii文件字段为空

时间:2013-05-14 05:47:20

标签: file-upload yii

我的文件字段与我的项目中所说的this链接完全一样,我把它的代码完全改变并且改变了所有看起来像它的东西,但是当我希望它的价值来自这个代码:

$uploadedFile = CUploadedFile::getInstanceByName($model,'image');

图片为空,我收到此错误:

  

isset中的非法偏移类型或空

我看到Yii在表单中为文件添加了两个字段,第一个是隐藏的,为什么是空的?

我假设Yii将它作为主图像,因此它总是空的,我怎么能得到文件字段值。

  • 唯一的区别是它使用CActiveRecord但我使用CFormModel因为我没有这个项目中的数据库。

  • 我可以通过$ model-> fieldname访问所有字段,但此字段为空!!

编辑:

我找到this教程,但我无法使用它,因为我的表单在控制器中,查看文件并从控制器获取了很多变量,这个教程表单看起来很干净:

 $form = new CForm('application.views.fileUpload.uploadForm', $model);

我的观点:

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

                            <div class="fieldtext">
                                <div class="labelbox">
                                    <?php echo $form->labelEx($model, Yii::t($this->language,'dialogboxname').":"); ?>
                                </div>
                                <?php echo $form->textField($model,'name',array('value'=>'aaa','size'=>10,'maxlength'=>50,'class'=>'inputbox')); ?>
                            </div>

                            <div class="fieldtext">
                                <div class="labelbox">
                                    <?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxemail').":"); ?>
                                </div>
                                <?php echo $form->textField($model,'email',array('value'=>'aaa@aa.com','size'=>17,'maxlength'=>50,'class'=>'inputbox')); ?>
                            </div>

                            <div class="fieldtext subjectbox">
                                <div class="labelbox">
                                    <?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxsubject').":"); ?>
                                </div>
                                <?php echo $form->textField($model,'subject',array('value'=>'aaa','size'=>36,'maxlength'=>50,'class'=>'inputbox')); ?>
                            </div>

                            <div class="fieldtext bodybox">
                                <div class="labelbox">
                                    <?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxbody').":"); ?>
                                </div>
                                <?php echo $form->textArea($model,'body',array('value'=>'aaa','rows'=>6, 'cols'=>35,'class'=>'inputbox')); ?>
                            </div>

                            <div class="fieldtext bodybox">

                                    <?php echo $form->labelEx($model,'image'); ?>

                                    <?php echo $form->fileField($model, 'image'); ?>
                            </div>

                            <?php if(CCaptcha::checkRequirements()): ?>
                            <div class="captchabox fieldtext">

                                <div>
                                <?php echo $form->labelEx($model,Yii::t($this->language,'dialogboxconfirmcode').":"); ?>
                                <?php $this->widget('CCaptcha',array(
                                        'showRefreshButton' => false,
                                        'clickableImage' => true,

                                    )); ?>
                                        <?php echo $form->textField($model,'verifyCode',array('size'=>5,'maxlength'=>5)); ?>

                                </div>

                            </div>
                            <?php endif; ?>
                            <div class="contactbuttom">
                                <div class="submitcontactform buttomsize">
                                    <?php echo CHtml::submitButton(Yii::t($this->language,'dialogboxsendbuttomn')); ?>
                                </div>
                            </div>
                        <?php $this->endWidget(); ?>

我的模特:

<?php
class ContactForm extends CFormModel {
    public $name;
    public $email;
    public $subject;
    public $body;
    public $image;
    public $verifyCode;

    /**
     * Declares the validation rules.
     */
    public function rules()
    {
        return array(
            // name, email, subject and body are required
            array('name, email, subject, body', 'required',
                    ),
            // email has to be a valid email address
            array('email', 'email'),

            array('image', 'file'),
    }

    public function attributeLabels()
    {
        return array(
            'verifyCode'=>'Verification Code',
        );
    }

}

我的控制器:

...
$this->model= new ContactForm;
$model = $this->model;

    if(isset($_POST['ContactForm']))
    {
        $model->attributes=$_POST['ContactForm'];

        if($model->validate())
        {
            $name = $model->name;
            $email = $model->email;
            $subject = $model->subject;
            $body = $model->body;

            $uploadedFile = CUploadedFile::getInstanceByName($model,'image');

....

有趣的是它检查规则但是说它是空的。

3 个答案:

答案 0 :(得分:1)

以下行错误:

$uploadedFile = CUploadedFile::getInstanceByName($model,'image');

http://www.yiiframework.com/doc/api/1.1/CUploadedFile#getInstanceByName-detail

你应该尝试:

$uploadedFile = CUploadedFile::getInstanceByName('ContactForm[image]');

或者:

$uploadedFile = CUploadedFile::getInstance($model,'image');

答案 1 :(得分:1)

您的代码:

$uploadedFile = CUploadedFile::getInstanceByName($model,'image');

必须替换为:

$uploadedFile = CUploadedFile::getInstance($model,'image');

CUploadedFile::getInstanceByName需要一个参数,而不是两个https://github.com/yiisoft/yii/blob/master/framework/web/CUploadedFile.php#L81

它需要文件输入的名称。所以,这样称呼:

CUploadedFile::getInstanceByName('ContactForm[image]'); //if 'ContactForm[image]' is input name

实际上,CUploadedFile::getInstance也可以使用它。但它也解析了输入名称https://github.com/yiisoft/yii/blob/master/framework/web/CUploadedFile.php#L57

所以,我相信,CUploadedFile::getInstance是更好的方法。

PS:不确定为什么使用CUploadedFile::getInstanceByName,即使您提到的教程在控制器中使用CUploadedFile::getInstance

要注意:)

答案 2 :(得分:0)

这是我的答案:

.table {
  height:100%;
  width:100%;
  display:table;   
}

.cell {
  display:table-cell;
  vertical-align:middle;
  height:100%;
}