/** * Creates a new ContentRecord model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate($label, $id) { $model = new ContentRecord(); if ($model->load(Yii::$app->request->post()) && $model->save()) { $imageName = $model->name; //get the instance of uploaded file $target = md5(uniqid()); $model->{$label} = UploadedFile::getInstance($model, $label); $model->{$label}->saveAs('Uploads/' . $target . '.' . $model->{$label}->extension); // save the path in db $model->name = $imageName . '.' . $model->{$label}->extension; $model->address = $target . '.' . $model->{$label}->extension; $model->ext = $model->{$label}->extension; $model->uploadedBy = Yii::$app->user->identity->getId(); if ($model->ext == 'jpg' || $model->ext == 'jpeg' || $model->ext == 'gif' || $model->ext == 'png') { $model->type = 'img'; } elseif ($model->ext == 'pdf') { $model->type = 'pdf'; } else { $model->type = 'video'; } $model->posted_at = time(); $model->flag = 1; $model->save(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'label' => $label, 'topic_id' => $id]); } }
public function actionCreate($label) { $model = new ContentRecord(); if ($model->load(Yii::$app->request->post()) && $model->save()) { $imageName = $model->name; //get the instance of uploaded file $model->{$label} = UploadedFile::getInstance($model, $label); $model->{$label}->saveAs('Uploads/' . $imageName . '.' . $model->{$label}->extension); // save the path in db $model->address = 'Uploads/' . $imageName . '.' . $model->{$label}->extension; $model->save(); return $this->redirect(['content/view', 'id' => $model->id]); } else { return $this->render('create', ['model' => $model, 'label' => $label]); } }