gpt4 book ai didi

file-upload - Yii 文件字段为空

转载 作者:行者123 更新时间:2023-12-02 02:02:29 24 4
gpt4 key购买 nike

我的文件字段与 this 完全一样链接在我的项目中说,我准确地输入了它的代码并改变了一切看起来像它但是当我想要它的值(value)时这个代码:

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

图像是空的,我得到这个错误:

Illegal offset type in isset or empty

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

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

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

  • 我可以通过 $model->fieldname 访问所有字段,但是这个字段是空的!!

编辑:

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

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

}

我的 Controller :

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

....

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

最佳答案

下面一行是错误的:

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

关于file-upload - Yii 文件字段为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16535964/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com