basename = "ImageController"; } /* * http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload */ public function processAction() { $this->strErrorDesc = ''; $this->strErrorHeader = ''; try { $this->requestMethod = $this->getServerRequestMethod(); $this->imageModel = new ImageModel(); switch($this->action) { case "upload": $response = $this->uploadImage(); break; case "update": $response = $this->updateImage(); break; case "delete": $response = $this->deleteImage(); break; default: $response = (object) ['Result' => 'Default']; $this->strErrorDesc = 'Controller Method not supported for processAction: ' . $this->action; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Entity'; break; } $responseData = json_encode($response); } catch (Error $e) { $this->strErrorDesc = $e->getMessage().' Something went wrong in processAction! Please contact support.'; $this->strErrorHeader = 'HTTP/1.1 500 Internal Server Error'; } // send output if (!$this->strErrorDesc) { $this->sendOutput( $responseData, array('Content-Type: application/json', 'HTTP/1.1 200 OK') ); } else { $this->sendOutput(json_encode(array('error' => $this->strErrorDesc)), array('Content-Type: application/json', $this->strErrorHeader) ); } } /* Upload the image and store on server as file */ private function uploadImage() { if ($this->checkRequestType('GET') == 'false') { return $this->unprocessableRequestResponse(); } return var_dump(file_get_contents('php://input')); $this->imagePayload = file_get_contents($_FILES['image']['tmp_name']); $this->imageModel->fileName = $_FILES['image']['name']; if(empty($this->imageModel->fileName)){ return $this->notFoundResponse(); } $this->imageModel->tempPath = $_FILES["image"]["tmp_name"]; $this->imageModel->basename = basename($this->imageModel->fileName); $this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename; $this->imageModel->fileType = pathinfo($this->imageModel->originalPath, PATHINFO_EXTENSION); if (! $this->validateImage()) { return $this->unprocessableEntityResponse(); } if (! $this->handleImage()) { return $this->notFoundResponse(); } $response = $this->imageModel->insertImage(); return $response; } private function handleImage() { $moved = false; if(!move_uploaded_file($this->imageModel->tempPath, $this->imageModel->originalPath)) { $moved = true; } return $moved; } private function validateImage(){ $validtion = false; if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){ $validtion = true; } return $validtion; } }