file-upload - 文件不在 yii2 中上传

标签 file-upload yii2

我想上传一张图片并将其保存到我的数据库中。 这里,数据库中的字段名称是image_path。当我尝试上传我的图像时,它显示错误:调用成员函数 saveAs() on a non-object on line

$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 ',            
    ];
}

这是我的 Controller :

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;
}

最佳答案

查看:

<?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 ',            
   ];
}

Controller :

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,
    ]);
}

关于file-upload - 文件不在 yii2 中上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34266454/

相关文章:

jquery - 页面加载时显示 jQuery 'waiting' gif

javascript - 如何在文件上传时显示加载图形?

jquery - Blueimp文件上传,如何知道进度回调是针对哪个文件的?

javascript - yii2:根据操作有条件显示/显示字段

php - 使用 Yii2 迁移添加多个附加列

node.js - 维梅奥 API : Upload from a File Using a Form

ruby-on-rails - 大文件上传

Yii2 不使用 gridview 没有结果消息

php - Yii 2 : Unable to send log via mail

php - 有没有办法改变 Yii2 中的 View 布局?