/** * Creates a new News model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new News(); $model->created_at = time(); $model->updated_at = time(); if ($model->load(Yii::$app->request->post()) && $model->save()) { return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }
/** * Creates a new News model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new News(); $time = time(); if ($model->load(Yii::$app->request->post())) { if (UploadedFile::getInstance($model, 'file')) { $uploaddir = Yii::getAlias('@frontend') . '/web/upload/upload_news/'; FileHelper::createDirectory($uploaddir); $file = \yii\web\UploadedFile::getInstance($model, 'file'); $file->saveAs($uploaddir . $time . '.' . $file->extension); $model->image = $time . '.' . $file->extension; } $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model]); } }