php - How to write the code for upload the image and save the image in database and the selected folder in Yii 2 -


please explain briefly code placed. , give 1 sample code save image in database , selected folder

i use code in models

public function aftersave($insert, $changedattributes) {     if (isset($this->varimage))     {         $this->varimage=uploadedfile::getinstance($this,'varimage');         if (is_object($this->varimage))         {             $path = yii::$app->basepath . '/uploads/';  //set directory path save image             $this->varimage->saveas($path.$this->intusertypeid."_".$this->varimage);   //saving img in folder             $this->varimage = $this->intusertypeid."_".$this->varimage;                     //appending id image name                         //\yii::$app->db->createcommand()             //->update('organization', ['logo' => $this->logo], 'id = "'.$this-   >id.'"')             //->execute(); //manually update image name db         }     } } 

please me try fix this.

thanks.

to save uploaded image in selected folder: (keep in mind $image in both examples uploadform instance https://github.com/yiisoft/yii2/blob/master/docs/guide/input-file-upload.md)

    $image->file = uploadedfile::getinstance( $image, 'file' );      if ($image->file && $image->validate())     {     $tmp = '/anyfolder/' . array_pop( explode( '/', $image->file->tempname ) );     $user->image = $tmp;     $user->save();     $image->file->saveas( yii::getalias( '@webroot' ) . $tmp . '.' . $image->file->extension );      } 

tempname helps save image unique name given system. code above saves relational path of $image in image( string keep address) field of $user.

now save uploaded image blob in database:

    $image->file = uploadedfile::getinstance( $image, 'file' );      if ($image->file)     {     $user->image_blob = file_get_contents( $image->file->tempname );     $user->save();      } 

Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -