我正在尝试在Yii上传简单的图像文件。但我提交的控制器上没有任何网址。 我在$ _FILES和$ _POST都没有得到任何东西。请检查我的以下代码我确定我在某个地方犯了一些小错误。
MODEL
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('image_url', 'required'),
array('image_url', 'file', 'types'=>'jpg, gif, png','on'=>'import'),
array('image_url', 'length', 'max'=>256),
// The following rule is used by search().
// @todo Please remove those attributes that should not be searched.
array('image_url', 'safe', 'on'=>'search'),
);
}
CONTROLER
public function actionImportVideo(){
$model=new Image();
print_r($_POST);
if(isset($_POST['image_url']))
{
print_r($_FILES);
$model->attributes=$_POST['image_url'];
$model->image_url=CUploadedFile::getInstance($model,'image_url');
if($model->save())
{
$model->image->saveAs('path/to/localFile');
// redirect to success page
}
}
$this->render('import_video', array('model'=>$model));
}
查看
<div class="form">
<?php
$form = $this->beginWidget(
'CActiveForm',
array(
'id' => 'upload-form',
'enableAjaxValidation' => false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)
);
?>
<div class="form-group">
<?php echo $form->labelEx($model,'image_url', array('class' => 'col-md-3 control-label')); ?>
<div class="col-md-9">
<?php echo $form->fileField($model,'image_url'); ?>
</div>
<div class="clear"></div>
</div>
<div class="form-actions">
<?php echo CHtml::submitButton('Submit', array('class' => 'btn red')); ?>
</div>
<?php $this->endWidget(); ?>
</div>
当我在控制器中打印$ _POST时,我得到了
Array ( [Image] => Array ( [image_url] => ) [yt0] => Submit )
请帮忙。在此先感谢