文件不在yii2上传

时间:2015-12-14 11:56:35

标签: file-upload yii2

我想上传图片并将其保存到我的数据库中。 这里,数据库中的字段名称是image_path。何时,我正在尝试上传我的图片时显示错误:在线上的非对象上调用成员函数saveAs()

$customer->file->saveAs('uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension);

如果我打印var_dump($customer->file);,则会返回 NULL

任何人都可以帮我解决这个问题。

这是我的观点:

    <?php $form = ActiveForm::begin([
            'id' => 'my-profile',
            'action' => \Yii::$app->urlManager->createUrl(['/myprofile', 'id_customer' => Yii::$app->user->identity->id_customer]),                     
            'options' => ['enctype'=>'multipart/form-data']
    ]); ?>
    <?= $form->field($customer, 'file')->fileInput() ?>
    <?php ActiveForm::end(); ?>

这是我的模特:

public $file;
public function rules()
{
    return [
        ['active', 'default', 'value' => self::STATUS_ACTIVE],
        ['active', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],            
        [['file'],'file'],
        [['name', 'image_path'], 'string', 'max' => 200],            
    ];
}

public function attributeLabels()
{
    return [            
        'file' => 'Profile Picture ',            
    ];
}

这是我的控制器:

public function actionMyprofile(){   

  $customer->file = UploadedFile::getInstance($customer,'image_path');      
  $customer->file->saveAs('uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension);
  $customer->file = 'uploads/customer/' . $customer->file->baseName . '.' . $customer->file->extension;
}

1 个答案:

答案 0 :(得分:2)

查看:

<?php $form = ActiveForm::begin([
        'id' => 'my-profile',
        'action' => \Yii::$app->urlManager->createUrl(['/myprofile', 'id_customer' => Yii::$app->user->identity->id_customer]),                     
        'options' => ['enctype'=>'multipart/form-data']
]); ?>
<?= $form->field($customer, 'image_path')->fileInput() ?>
<?php ActiveForm::end(); ?>

<强>型号:

public function rules()
{
  return [
    ['active', 'default', 'value' => self::STATUS_ACTIVE],
    ['active', 'in', 'range' => [self::STATUS_ACTIVE, self::STATUS_DELETED]],            
    [['image_path'],'file'],
    [['name', 'image_path'], 'string', 'max' => 200],            
  ];
}

public function attributeLabels()
{
   return [            
       'image_path' => 'Profile Picture ',            
   ];
}

<强>控制器:

public function actionCreate()
{
    $model = new YourModel_name();          

    if ($model->load(Yii::$app->request->post())) {
        $model->image_path = UploadedFile::getInstance($model, 'image_path');

        $filename = pathinfo($model->image_path , PATHINFO_FILENAME);
        $ext = pathinfo($model->image_path , PATHINFO_EXTENSION);

        $newFname = $filename.'.'.$ext;

        $path=Yii::getAlias('@webroot').'/image/event-picture/';
        if(!empty($newFname)){
            $model->image_path->saveAs($path.$newFname);
            $model->image_path = $newFname; 
            if($model->save()){
                return $this->redirect(['your redirect_path', 'id' => $model->id]);
            }       
        }           
    } 
    return $this->render('create', [
        'model' => $model,
    ]);
}