/**
  * 上传头像
  */
 public function actionUploadFace()
 {
     //@todo
     $model = new UploadForm();
     if (Yii::$app->request->isPost) {
         $model->file = UploadedFile::getInstance($model, 'file');
         if ($model->file && $model->validate()) {
             $path = time() . '_' . Yii::$app->security->generateRandomString(8) . '.' . $model->file->extension;
             $model->file->saveAs(Yii::$app->basePath . '/web/uploads/' . $path);
             $uploadFile = new UploadFile();
             $uploadFile->path = $path;
             $uploadFile->user_id = Yii::$app->user->id;
             $uploadFile->mime_type = $model->file->type;
             if ($uploadFile->save()) {
                 $user = User::findOne(['id' => Yii::$app->user->id]);
                 if ($user->face) {
                     $user->face = null;
                     $user->save();
                 }
                 $user->face = $path;
                 if ($user->save()) {
                     return $this->redirect('index');
                 } else {
                     error_log(print_r($user->errors, true));
                     $uploadFile->delete();
                 }
             } else {
                 error_log(print_r($uploadFile->errors, true));
             }
         }
     }
     return $this->render('upload-face', ['model' => $model]);
 }
 public function actionRoot()
 {
     $model = new UploadFile();
     if (Yii::$app->request->isPost) {
         // Загрузка пост параметров в модель
         $model->load(Yii::$app->request->post());
         $model->file = UploadedFile::getInstance($model, 'file');
         //print'<pre>'; print_r($model->file); print'</pre>';
         if ($model->upload()) {
             // Скрипт
             $data = Sorting::root('uploads/' . $model->file->name, $model->text, $error);
             // file is uploaded successfully
             return $this->render('root', ['model' => $model, 'data' => $data, 'error' => isset($error) ? $error : false]);
         }
     }
     return $this->render('root', ['model' => $model, 'data' => $data]);
 }
Example #3
0
 public function uploadAvatar($path)
 {
     if ($this->validate()) {
         try {
             FileHelper::createDirectory($path, $mode = 509, $recursive = true);
             //var_dump(FileHelper::findFiles($path));
             $this->imageFile->saveAs($path . '/avatar.' . $this->imageFile->extension);
             //Ajouter nouvel UploadFile() dans la BD -- Manque plus qu'à save
             $file = new UploadFile();
             $name = 'avatar';
             $filename = 'avatar.' . $this->imageFile->extension;
             return $file->addFile($this->imageFile, $name, $filename);
         } catch (Exception $ex) {
             echo "Exception upload_file : " . $ex->getMessage();
         }
     } else {
         return false;
     }
 }
Example #4
0
 public function getAvatar()
 {
     switch ($this->active_avatar) {
         case 0:
             $path = '/uploads/' . sha1('base') . '/avatar.jpg';
             break;
         case 1:
             //var_dump($this);die;
             $avatar = UploadFile::findIdentity($this->avatar);
             //Bug sur Ajax $this->avatar = null
             $path = '/uploads/' . sha1($this->getUser()->username) . '/' . $avatar->filename;
             break;
         case 2:
             $path = $this->getGravatar(200);
             break;
         default:
             $path = '/uploads/' . sha1('base') . '/avatar.jpg';
             break;
     }
     return $path;
 }
 /**
  * 
  */
 public function actionChangeAvatar($id_user = null)
 {
     $model = new UploadForm();
     //Récupération du dossier frontend/web/uploads/sha1(login)/
     if ($id_user == NULL) {
         $user = Yii::$app->user->identity;
     } else {
         $user = User::findIdentity($id_user);
     }
     $accountUser = $user->findAccount();
     if ($accountUser->getAvatar() != NULL) {
         $avatar = UploadFile::findIdentity($accountUser->avatar);
     } else {
         $avatar = NULL;
     }
     //var_dump($avatar);die;
     if (Yii::$app->request->isPost) {
         //Chemin du dossier upload de l'User
         $hash = sha1($user->username);
         $path = 'uploads/' . $hash;
         $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
         if ($id = $model->uploadAvatar($path)) {
             //Association user
             $accountUser->updateAvatar($id);
             // file is uploaded successfully
             Yii::$app->getSession()->setFlash('success', 'Avatar changé');
             return $this->redirect(['index']);
         } else {
             echo "Echec de l'upload";
         }
     }
     return $this->renderAjax('change-avatar', ['model' => $model, 'user' => $user, 'avatar' => $avatar]);
 }