public function upload($userId, $foldername = NULL) { if ($this->validate()) { $attachments = []; $i = 0; foreach ($this->imageFiles as $file) { $orignalname = $file->baseName . '.' . $file->extension; $manupolated_name = md5($file->baseName) . '.' . $file->extension; $model = new Attachments(); $model->user_id = $userId; $model->orignal_name = $orignalname; $model->manupulated_name = $manupolated_name; $model->save(); if ($foldername == NULL) { $file->saveAs('../../profiles/' . $manupolated_name); } else { $file->saveAs('../../profiles/' . $foldername . '/' . $manupolated_name); } $attachments[$i] = $model->id; } return $attachments; } else { return false; } }
/** * Updates an existing TravellerPhoto 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); $modelAttachments = Attachments::findOne($model->attachment_id); $uploadModel = new UploadImagesForm(); $traveller_id = $model->traveller_id; $traveldescription = TravellerDescription::find()->where(['traveller_id' => $traveller_id])->one(); if ($uploadModel->load(Yii::$app->request->post()) && $model->load(Yii::$app->request->post())) { $traveller = Travellers::findOne($traveller_id); if (isset($uploadModel->uploadFiles)) { $uploadImagePath = Yii::$app->basePath . "/../profiles/travellers/"; $img = str_replace(' ', '+', str_replace('data:image/png;base64,', '', $uploadModel->uploadFiles)); $data = base64_decode($img); $filename = uniqid() . '.png'; $file = $uploadImagePath . $filename; if (!is_dir($uploadImagePath)) { mkdir($uploadImagePath); } $success = file_put_contents($file, $data); if ($success) { $orignalname = $filename; $manupolated_name = md5(uniqid()) . '.png'; $modelAttachments = new Attachments(); $modelAttachments->user_id = $traveller->user_id; $modelAttachments->orignal_name = $orignalname; $modelAttachments->manupulated_name = $manupolated_name; if ($modelAttachments->save()) { $model->attachment_id = $modelAttachments->id; } } } if ($model->save(false)) { if (count($traveldescription) > 0) { return $this->redirect(['travellerdescription/update', 'id' => $traveldescription->id]); } else { return $this->redirect(['travellerdescription/create', 'traveller_id' => $model->traveller_id]); } } } else { return $this->render('update', ['model' => $model, 'uploadModel' => $uploadModel, 'traveller_id' => $traveller_id, 'traveldescription' => $traveldescription, 'modelAttachments' => $modelAttachments]); } }