我是Yii的新手,所以不要生气。我无法弄清楚CUploadFile是如何工作的。我有我的观点文件:
<div class="form form-custom">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'image-temp-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('enctype'=>'multipart/form-data' ),
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'car_type'); ?>
<?php
$this->widget('CMultiFileUpload', array(
'model'=>$model,
'attribute'=>'car_type',
//'name' => 'files',
'accept'=>'jpg|gif|png',
'denied'=>'File is not allowed',
'max'=>3,
));
?>
<?php echo $form->error($model,'car_type'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
我的控制器分解为位:
public function actionCreate()
{
$model=new EeCarTypes;
if ( isset( $_FILES['EeCarTypes']))
{
$images = $_FILES['EeCarTypes'];
$model->car_type = $images['name']['car_type'][0];
$uploadedFile = CUploadedFile::getInstance($model, 'car_type');
CVarDumper::Dump($uploadedFile, 10 ,true);
$uploadedFile->saveAs(Yii::app()->basePath . '/../images/upload/cartypes/'.'hehehe');
}
$this->render('create',array(
'model'=>$model,
));
}
和我的模特:
public $ car_type;
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
// array('image', 'file','types'=>'jpg, gif, png', 'allowEmpty'=>true, 'on'=>'update'),
array('car_type', 'length', 'max'=>255),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('id, car_type', 'safe', 'on'=>'search'),
);
}
在我尝试上传到控制器之前,外翻会顺利进行。我在CUploadedFile :: getInstance()中作为参数放置的所有东西;结果为NULL;
答案 0 :(得分:0)
必须为文件变量指定强制类型为file的规则。
array('car_type', 'file', 'types'=>'jpg, gif, png')
参考 http://www.yiiframework.com/wiki/2/how-to-upload-a-file-using-a-model/