public function actionUpload($documentID = null) { Yii::$app->response->format = Response::FORMAT_JSON; $result = ['success' => false]; $uploadForm = new UploadForm(); $uploadForm->file = UploadedFile::getInstanceByName('file'); $uploadForm->documentID = $documentID; if ($uploadForm->validate() && $uploadForm->upload()) { $result['success'] = true; } return $result; }
/** * Creates a new Books model. * If creation is successful, the browser will be redirected to the 'view' page. * @return mixed */ public function actionCreate() { $model = new Books(); $upload = new UploadForm(); $upload->imageFile = UploadedFile::getInstance($model, 'img'); if ($upload->upload()) { $model->status = 'available'; if ($model->load(Yii::$app->request->post())) { $model->img = 'images/' . $upload->imageFile->baseName . '.' . $upload->imageFile->extension; if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } } } return $this->render('create', ['model' => $model]); }
public function actionUpdateProfile() { if (!Yii::$app->user->isGuest) { if (Yii::$app->request->get('new')) { $model = new Profile(); $avatar = ""; $user_id = Yii::$app->user->id; } else { if (Yii::$app->request->get('id')) { $user_id = Yii::$app->request->get('id'); } else { $user_id = Yii::$app->user->id; } $model = Profile::find()->where(['user_id' => $user_id])->one(); if (!empty($model->avatar)) { $avatar = $model->avatar; } } if (Yii::$app->request->post()) { $model->user_id = $user_id; $model->first_name = Yii::$app->request->post('Profile')['first_name']; $model->second_name = Yii::$app->request->post('Profile')['second_name']; $model->last_name = Yii::$app->request->post('Profile')['last_name']; $model->birthday = Yii::$app->request->post('Profile')['birthday']; $model->about = Yii::$app->request->post('Profile')['about']; $model->location = Yii::$app->request->post('Profile')['location']; $model->sex = Yii::$app->request->post('Profile')['sex']; $upload = new UploadForm(); if ($upload->imageFiles = UploadedFile::getInstances($model, 'avatar')) { if (!empty($avatar)) { if (file_exists(Yii::getAlias('@webroot/uploads/image_profile/original/' . $avatar))) { unlink(Yii::getAlias('@webroot/uploads/image_profile/original/' . $avatar)); unlink(Yii::getAlias('@webroot/uploads/image_profile/xm/' . $avatar)); unlink(Yii::getAlias('@webroot/uploads/image_profile/sm/' . $avatar)); unlink(Yii::getAlias('@webroot/uploads/image_profile/lm/' . $avatar)); } } $filename = $upload->upload('image_profile/'); $model->avatar = $filename[0] . '.' . $upload->imageFiles[0]->extension; } else { $model->avatar = $avatar; } if ($model->save()) { Yii::$app->session->setFlash('success', "Профиль изменен"); return $this->redirect(['/user/view-profile', 'id' => $user_id]); } else { Yii::$app->session->setFlash('error', "Не удалось изменить профиль"); Yii::error('Ошибка записи. Профиль не изменен'); return $this->refresh(); } } else { return $this->render('update-profile', ['model' => $model]); } } else { Yii::$app->session->setFlash('error', 'Нет доступа!'); return $this->redirect('/'); } }
protected function isUploaded($model) { $up = new UploadForm(); $up->cover = UploadedFile::getInstance($model, 'cover'); $up->carousel = UploadedFile::getInstances($model, 'carousel'); if ($this->source = $up->upload()) { $arr = $this->source; $model->cover = $arr['0']; $model->one = $arr['1']; $model->two = $arr['2']; $model->three = $arr['3']; $model->four = $arr['4']; $model->five = $arr['5']; // 封面图片处理,有首页大图小图,期刊页列表图缩略图 $big = Yii::getAlias('@app') . '/uploads/big' . pathinfo($arr['0'])['basename']; $small = Yii::getAlias('@app') . '/uploads/small' . pathinfo($arr['0'])['basename']; $list = Yii::getAlias('@app') . '/uploads/list' . pathinfo($arr['0'])['basename']; $thumb = Yii::getAlias('@app') . '/uploads/thumb' . pathinfo($arr['0'])['basename']; Image::thumbnail($arr['0'], 640, 426)->save($big, ['quality' => 80]); Image::thumbnail($arr['0'], 303, 202)->save($small, ['quality' => 80]); Image::thumbnail($arr['0'], 540, 393)->save($list, ['quality' => 80]); Image::thumbnail($arr['0'], 46, 30)->save($thumb, ['quality' => 80]); } if ($model->save()) { return $this->redirect(['view', 'id' => $model->id]); } }
/** * 编辑资料页面 * If update is successful, the browser will be redirected to the 'view' page. * * @param integer $id * @return mixed */ public function actionEdit() { $model = $this->findModel(Yii::$app->user->identity->getId()); if (Yii::$app->request->isPost) { $post = Yii::$app->request->post(); $up = new UploadForm(); $up->photo = UploadedFile::getInstance($model, 'photo'); if ($up->photo) { $path = $up->upload($model->id); $post['User']['photo'] = $path; } else { $post['User']['photo'] = $model->photo; } if ($model->load($post) && $model->save()) { return $this->redirect(['edit']); } } else { return $this->render('update', ['model' => $model]); } }
public function actionUpload() { Yii::$app->response->format = Response::FORMAT_JSON; $document = $this->findModel(Yii::$app->request->post('id')); $model = new UploadForm(); if (Yii::$app->request->isAjax) { $model->files = UploadedFile::getInstancesByName('files'); $model->document_id = $document->id; if ($model->upload()) { return ['fileuploaded' => 'true']; } else { return $model->errors; } } }
public function actionUpdate($id) { if (!Yii::$app->user->can('product/update')) { Yii::$app->session->setFlash('error', 'Нет доступа!'); return $this->redirect('/'); } $categories = Product::loadCategory(); $model = $this->findModel($id); if ($model->load(Yii::$app->request->post()) && $model->save()) { Images::deleteImages($model); $upload = new UploadForm(); $upload->imageFiles = UploadedFile::getInstances($model, 'images'); if ($filename = $upload->upload('image_product/')) { Images::loadImages($upload, $filename, $model->id); } $model->sizeImages(); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model, 'categories' => $categories]); } }