3

我的文件字段与链接在我的项目中所说的完全一样,我准确地输入了它的代码并更改了所有内容,但是当我希望通过此代码获得它的值时:

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

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

isset 中的非法偏移类型或为空

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

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

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

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

编辑:

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

 $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');

....

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

4

3 回答 3

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');
于 2013-05-15T13:58:30.063 回答
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在控制器中使用。

留心 :)

于 2013-05-15T14:17:51.877 回答
0

这是我的回答:

     public function actionCreate()
    {
        $model = new Answer();
        var_dump(Yii::$app->request->post());`enter code here`
        if ($model->load(Yii::$app->request->post()) && $model->validate()) {
            $model->answer_by= \Yii::$app->user->getId();
            $name = $model->answer_attachment;
            var_dump($name);
            $model->answer_attachment = UploadedFile::getInstanceByName('btn.jpg');
            var_dump($model->answer_attachment);
             $time=time();
            if(!empty($model->answer_attachment)){
                $model->answer_attachment->saveAs('images/answer/'.$time.'.'.$model->answer_attachment->extension);
                $model->answer_attachment='images/answer/'.$time.'.'.$model->answer_attachment->extension;
            }
            $model->answer_timestamp=$time;
            //$model->save();
            //return $this->redirect(['view', 'id' => $model->answer_id]);
        } else {
            return $this->render('create', [
                'model' => $model,
            ]);
        }
    }
于 2016-03-01T15:01:33.920 回答