它显示的错误类似于"未定义的变量:fullImgSource"。
任何人都可以帮我吗?
视野
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'Candidate-form',
'enableClientValidation'=>true,
'clientOptions'=>array(
'validateOnSubmit'=>true
),
'htmlOptions' => array('enctype' => 'multipart/form-data')
));
?>
<p class="note">Fields with <span class="required">*</span> are required. </p>
<?php echo $form->errorSummary($model); ?>
<?php
echo $form->labelEx($model, 'delete_YN');
echo $form->fileField($model, 'delete_YN');
echo $form->error($model, 'delete_YN');
?>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Add' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->
在控制器代码中
Uploadfile
$model = new Candidate;
if(isset($_POST['Candidate']))
{
$model->attributes=$_POST['Candidate'];
$name=@$_FILES["Candidate"]["name"]["delete_YN"];
$model->delete_YN = CUploadedFile::getInstance($model,'delete_YN');
if($model->save())
$fullImgSource = Yii::getPathOfAlias('webroot').'/upload/'.$name;
$model->delete_YN->saveAs($fullImgSource);
$model->delete_YN = $name;
$model->save();
$this->redirect(array('view','id'=>$model->id));
}
$this->render('create',array('model'=>$model,));
答案 0 :(得分:0)
答案 1 :(得分:0)
您的$模型很可能没有通过验证(如果验证失败,请检查$ model-&gt; getErrors()以获取有关确切失败的更多信息),因此save()方法不会通过,$ fullImgSource最终不会被定义。您应该在save方法之后将代码包装在大括号中,因为以下代码依赖它来成功。
$cUploadedFile = CUploadedFile::getInstance($model, 'delete_YN');
if ($cUploadedFile) {
$model->delete_YN = $cUploadedFile;
}
if ($model->save()) {
if ($cUploadedFile) {
// Save file from temp to a specific location
$cUploadedFile->saveAs('upload/' . $model->delete_YN);
}
$this->redirect(array('view','id'=>$model->id));
}
希望这能解决您收到错误的原因。现在,关于如何实际处理上传,您不必访问$ _FILES来上传。 CUploadedFile :: getInstance($ model,'attribute_name')应该为你处理。