Php 上传图片

Php 上传图片,php,yii,Php,Yii,在我的web应用程序中,我需要在创建记录时上载图像。但我得到了错误,尽管我在遵循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

在我的web应用程序中,我需要在创建记录时上载图像。但我得到了错误,尽管我在遵循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(); ?>

有谁能帮我解决这个问题。

您是否在您的模型中将图像设置为文件

public function rules()
{
    return array(
        array('image', 'file', 'types'=>'jpg, gif, png', 'safe'=>true),
    );
}
在控制器中,将线路更改为

$model->image->saveAs(Yii::app()->baseUrl.'/images/vegetables/'.$model->image);

@Burhan Cetin我终于明白了。当我们上传时,我们应该使用

$model->image->saveAs(Yii::getPathOfAlias('webroot').'/images/vegetables/'.$model->image);

因为在上传时,我们应该指定文件的完整路径(/var/www)。当我们在视图中渲染已经存在的图像时,我们使用“baseUrl”。

Y我这样做了。我已在模型规则中声明,在您的模型中是否已将图像变量设置为公共?公众形象;我编辑了答案。你在模型中添加了“safe”=>“true”了吗?我添加了它,但是现在,当我单击“创建”将其定向到空白页时,我在文件夹中也看不到图像。此外,也不会添加记录。请检查您设置为上传文件夹的文件夹权限。chmod 777-R您的上载文件夹
$model->image->saveAs(Yii::getPathOfAlias('webroot').'/images/vegetables/'.$model->image);