nenad-zivkovic / yii2-advanced-template

Yii2 improved advanced application template that can bake you cookies
BSD 3-Clause "New" or "Revised" License
93 stars 66 forks source link

How can i upload to uploads #29

Closed ddeshar closed 6 years ago

ddeshar commented 8 years ago

i am trying to upload photo to upload file but i am not abling to do this here is my uploading code

######### start upload photo############
   public $upload_foler ='banner';

    public function upload($model,$attribute){
        $photo  = UploadedFile::getInstance($model, $attribute);
          $path = $this->getUploadPath();
        if ($this->validate() && $photo !== null) {
            $fileName = md5($photo->baseName.time()) . '.' . $photo->extension;
            //$fileName = $photo->baseName . '.' . $photo->extension;
            if($photo->saveAs($path.$fileName)){
              return $fileName;
            }
        }
        return $model->isNewRecord ? false : $model->getOldAttribute($attribute);
    }

    public function getUploadPath(){
        return Yii::getAlias(''@uploads').'/'.$this->upload_foler.'/';
    }

    public function getUploadUrl(){
        return Yii::getAlias('@uploads').'/'.$this->upload_foler.'/';
    }

    public function getPhotoViewer(){
      return empty($this->photo) ? Yii::getAlias('@uploads').'/img/none.png' : $this->getUploadUrl().$this->photo;
    }
    ###### finish upload photo ######

so what my alias must be from which i can upload my photo?

ghost commented 7 years ago

Im not sure but would want you to check public function getUploadPath(){ return Yii::getAlias(''@uploads').'/'.$this->upload_foler.'/'; }

You got double quote before @uploads

I changed it to public function getUploadPath(){ return Yii::getAlias('@uploads').'/'.$this->upload_foler.'/'; }

And it worked for me.