在yii2文件上传中的非对象上调用成员函数saveAs()

时间:2016-09-06 07:27:41

标签: php file file-upload yii2 yii2-advanced-app

我正在使用kartik fileUpload扩展名来上传yii2中的文件,一旦我选择了一个文件并提交,我就会

  

在非对象

上调用成员函数saveAs()

我已经检查了有关此问题的其他帖子,但它没有帮助,

我的查看代码..

 <?php  echo $form->field($documents, 'sars_certificate')->label(false)->
  widget(FileInput::classname(), [
    'pluginOptions' => [
        'showCaption' => true,'showRemove' => true,'showClose' => true,
        'showPreview' => true,'uploadAsync' => true,
        'showUpload' => false,'maxFileSize'=> 2000,'autoReplace'=> true, 
        'placeholder' => 'Select a File...',],]); ?>

我的型号代码..

 [['sars_certificate'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg,pdf,jpeg']

我的控制器代码..

  public function actionCreate()
  {   
  $model = new Projects(); 
  $documents = new ProjectDocuments();    

$borrower_id = Yii::$app->user->identity->id;
  $code = $model->accessCode(10);
  if ($model->load(Yii::$app->request->post())) 
  {
   //All $model related things will bw done

  //In this im doing file upload
  $documents->borrower_id = $borrower_id; 
  $documents->project_id = $code;    
  $documents->save(false);  

  $documents->sars_certificate = UploadedFile::getInstances($documents,'sars_certificate');

  $documents->sars_certificate->saveAs('user/purchase_order/' . $documents->sars_certificate->baseName . '.' . $documents->sars_certificate->extension);
  $documents->sars_certificate = $documents->sars_certificate;
  $documents->save(false); 

         return $this->redirect(['index']);
    } 

我已经给了if($documents->validate()){ **** }。但它不会因为条件本身而受到影响,所以我将其删除了。现在它说上面的错误..

请有人帮忙,我浪费了很多时间......

1 个答案:

答案 0 :(得分:1)

1)为避免重复图片,请在图片名称中添加time()

2)$documents->save(false);之前删除UploadInstance

public function actionCreate() {
  $model = new Projects();
  $documents = new ProjectDocuments();

  $borrower_id = Yii::$app->user->identity->id;
  $code = $model->accessCode(10);
  if ($model->load(Yii::$app->request->post())) {
    //All $model related things will bw done
    //In this im doing file upload
    $documents->borrower_id = $borrower_id;
    $documents->project_id = $code;


    if(UploadedFile::getInstance($documents, 'sars_certificate')){
      $image = UploadedFile::getInstance($documents, 'sars_certificate'); 
      $imageName = time().$image->name;
      $path = "user/purchase_order/".$imageName; 
      if($image->saveAs($path)){
       $documents->sars_certificate = $imageName;
      }
    }

    $documents->save(false);
    return $this->redirect(['index']);
  }
}

更新

public function actionCreate() {
  $model = new Projects();
  $documents = new ProjectDocuments();

  $borrower_id = Yii::$app->user->identity->id;
  $code = $model->accessCode(10);
  if ($model->load(Yii::$app->request->post())) {
    //All $model related things will bw done
    //In this im doing file upload
    $documents->borrower_id = $borrower_id;
    $documents->project_id = $code;


    if(UploadedFile::getInstance($documents, 'sars_certificate')){
      $image = UploadedFile::getInstance($documents, 'sars_certificate');
      if($image){
        $imageName = time().$image->name;
        $path = "user/purchase_order/".$imageName; 
        if($image->saveAs($path)){
         $documents->sars_certificate = $imageName;
        }
      }
    }

    $documents->save(false);
    return $this->redirect(['index']);
  }
}