Пример #1
0
 /**
  * Creates a new RecommendData model.
  * If creation is successful, the browser will be redirected to the 'view' page.
  * @return mixed
  */
 public function actionCreate()
 {
     $model = new RecommendData();
     if ($model->load(Yii::$app->request->post())) {
         $model->img = UploadedFile::getInstance($model, 'img');
         $model->pubtime = time();
         if ($model->validate()) {
             if ($model->img) {
                 //$path = Yii::$app->params['recommendUploadPath'] . '/'. date('Y') . '/' . date('m') . '/' . date('d');
                 $path = Yii::$app->params['recommendUploadPath'];
                 $imgName = $path . date('Ymdhis') . rand(1000, 9999) . '.' . $model->img->extension;
                 $model->img->saveAs(Yii::getAlias('@frontend/web') . DIRECTORY_SEPARATOR . $imgName);
                 $model->img = $imgName;
             }
             $model->save(false);
             return $this->redirect(['view', 'id' => $model->id]);
         } else {
             return $this->render('create', ['model' => $model]);
         }
     } else {
         return $this->render('create', ['model' => $model]);
     }
 }