imageModel = new ImageModel(); $this->requestMethod = $this->getServerRequestMethod(); } /* * http://localhost/CustomerRewardsRESTAPI/index.php/image/process/upload */ public function processAction() { $this->strErrorDesc = ''; $this->strErrorHeader = ''; try { 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) ); } } private function uploadImage(){ if ($this->checkRequestType('POST') == 'false') { $this->strErrorDesc = 'Request Method not supported for processAction'; $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Request'; return; } // reads the raw POST data and returns it as a string. $jsonPayload = file_get_contents('php://input'); $input = json_decode($jsonPayload, TRUE); if (! $this->validatePerson($input)) { return $this->unprocessableEntityResponse(); } $response = $this->insertImage(); return $response; } private function insertImage() { header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Methods: POST"); header("Content-Type: application/json"); if (isset($_FILES['image'])) { $uploadImage = $this->processImage(); $success = false; if (!$uploadImage['error']) { } } } private function processImage() { $error = false; $msg = null; $uploadTo = "public/images/"; $allowFileType = array('jpg','png','jpeg'); $fileName = $_FILES['image']['name']; $tempPath = $_FILES["image"]["tmp_name"]; $basename = basename($fileName); $originalPath = $uploadTo.$basename; $fileType = pathinfo($originalPath, PATHINFO_EXTENSION); if(!empty($fileName)){ if(in_array($fileType, $allowFileType)){ if(!move_uploaded_file($tempPath, $originalPath)){ $msg = 'Image Not uploaded ! try again'; $error = true; } } else { $msg = 'Image type is not allowed'; $error = true; } } else { $msg = 'Image is required'; $error = true; } $imageInfo = [ "error" => $error, "msg" => $msg, "filename" => $fileName, "filepath" => $originalPath ]; return $imageInfo; } }