在我的Web應用程序中,我需要在創建記錄時上傳圖片。但我收到錯誤,雖然我遵循yii論壇中給出的指示 這是我得到的錯誤。Yii上傳圖片
Application Log
Timestamp Level Category Message
16:50:00.207452 warning application
Failed to set unsafe attribute "image" of "Vegetable".
in
/var/www/affm_web_v1r2_xxxx_untouched/protected/controllers/VegetableController.php
(29)
in /var/www/affm_web_v1r2_xxxxuntouched/index.php (7)
我正在使用對話框來創建新記錄。 我對控制器
public function actionCreate()
{
$model=new Vegetable;
if (Yii::app()->request->isAjaxRequest)
{
$this->renderPartial('create', array('model'=>$model, 'asDialog'=>!empty($_GET['asDialog']),), false, true);
Yii::app()->user->setReturnUrl($_GET['returnUrl']);
Yii::app()->end();
}
else
{
if(isset($_POST['Vegetable']))
{
$model->attributes=$_POST['Vegetable'];
$model->image=CUploadedFile::getInstance($model,'image');
if($model->save())
{
$model->image->saveAs((Yii::app()->baseUrl.'/images/vegetables/').$model->image);
$this->redirect(Yii::app()->user->returnUrl);
}
// $this->render('create', array('model'=>$model));
}
}
我的視圖頁面
<?php $form=$this->beginWidget('bootstrap.widgets.TbActiveForm',array(
'id'=>'vegetable-form',
'enableAjaxValidation'=>false,
'htmlOptions' => array('enctype' => 'multipart/form-data'),
)); ?>
<p class="help-block">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<?php echo $form->textFieldRow($model,'name',array('class'=>'span5','maxlength'=>64)); ?>
<?php echo $form->textFieldRow($model,'code',array('class'=>'span5','maxlength'=>64)); ?>
<?php echo $form->textFieldRow($model,'img_name',array('class'=>'span5','maxlength'=>64)); ?>
<?php
echo $form->labelEx($model, 'image');
echo $form->fileField($model, 'image');
echo $form->error($model, 'image');
?>
<div class="form-actions">
<?php $this->widget('bootstrap.widgets.TbButton', array(
'buttonType'=>'submit',
'type'=>'primary',
'label'=>$model->isNewRecord ? 'Create' : 'Save',
)); ?>
</div>
<?php $this->endWidget(); ?>
代碼代碼,我試圖改變屬性安全的,但我得到這個錯誤。
include(image.php): failed to open stream: No such file or directory
任何人都可以幫我解決這個問題。
嘗試,但我得到相同的錯誤。 – user2492854
對不起,我編輯了答案。 –
是的,我做過了。我已經在模型規則 – user2492854