コード例 #1
0
ファイル: PostController.php プロジェクト: GHubgenius/dcms
 public function actionCreateImgAjax()
 {
     if (!empty($_FILES)) {
         $imageType = array('.gif', '.jpg', '.jpeg', '.png');
         if (!in_array(strrchr(strtolower($_FILES['imgFile']['name']), '.'), $imageType)) {
             echo Json::encode(['error' => 1, 'message' => Yii::t('app', "that's not an image, only allow '.gif', '.jpg', '.jpeg', '.png'")]);
             Yii::$app->end();
         }
         $dir = BASE_PATH . '/upload/post/' . date('Ym') . '/';
         if (!is_dir($dir)) {
             @mkdir(dirname($dir), 0777);
             @mkdir($dir, 0777);
             touch($dir . '/index.html');
         }
         $name = date('His') . strtolower(Common::random(16)) . strrchr($_FILES['imgFile']['name'], '.');
         $tmp_name = $_FILES['imgFile']['tmp_name'];
         move_uploaded_file($tmp_name, $dir . $name);
         $url = Yii::$app->homeUrl . 'upload/post/' . date('Ym') . '/' . $name;
         $name = $_FILES['imgFile']['name'];
         $size = $_FILES['imgFile']['size'];
         echo Json::encode(['error' => 0, 'url' => $url]);
     } else {
         echo Json::encode(['error' => 1, 'message' => Yii::t('app', "upload error")]);
     }
     Yii::$app->end();
 }
コード例 #2
0
ファイル: UserController.php プロジェクト: rocketyang/dcms2
 public function actionView($id)
 {
     $model = $this->findModel($id);
     $userform = new UserForm();
     $userform->username = $model->username;
     $userform->email = $model->email;
     if ($userform->load(Yii::$app->request->post()) && $userform->validate()) {
         $types = ['.gif', '.jpg', '.png'];
         $image = UploadedFile::getInstance($userform, 'avatar');
         if (!empty($image->name) && in_array(strrchr(strtolower($image->name), '.'), $types)) {
             $dir = BASE_PATH . '/upload/avatar/';
             if (!is_dir($dir)) {
                 @mkdir($dir, 0777);
                 touch($dir . '/index.html');
             }
             $name = date('His') . strtolower(Common::random(16)) . strrchr($image->name, '.');
             $image->saveAs($dir . $name);
             $model->avatar = Yii::$app->homeUrl . 'upload/avatar/' . $name;
         }
         if (!empty($userform->username)) {
             $model->username = $userform->username;
         }
         if (!empty($userform->password)) {
             $model->password = $userform->password;
         }
         if (!empty($userform->email)) {
             $model->email = $userform->email;
         }
         if ($model->save()) {
             Yii::$app->getSession()->setFlash('success', '保存成功.');
         } else {
             Yii::$app->getSession()->setFlash('danger', '保存失败.');
         }
         return $this->redirect(['view', 'id' => $model->id]);
     } else {
         return $this->render('view', ['model' => $model, 'userform' => $userform]);
     }
 }
コード例 #3
0
ファイル: LinkController.php プロジェクト: rocketyang/dcms2
 /**
  * Updates an existing Link model.
  * If update is successful, the browser will be redirected to the 'view' page.
  * @param integer $id
  * @return mixed
  */
 public function actionUpdate($id)
 {
     $model = $this->findModel($id);
     if ($model->load(Yii::$app->request->post())) {
         $image = UploadedFile::getInstance($model, 'logo');
         if (!empty($image->name)) {
             $dir = BASE_PATH . '/upload/link/';
             if (!is_dir($dir)) {
                 @mkdir(dirname($dir), 0777);
                 @mkdir($dir, 0777);
                 touch($dir . '/index.html');
             }
             $name = date('His') . strtolower(Common::random(16)) . strrchr($image->name, '.');
             $image->saveAs($dir . $name);
             $model->logo = 'upload/link/' . $name;
         }
         if ($model->save()) {
             return $this->redirect(['view', 'id' => $model->id]);
         }
     } else {
         return $this->render('update', ['model' => $model]);
     }
 }