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('POST') == 'false') { return $this->unprocessableRequestResponse(); } /* * array(1) { ["MemberImageModel"]=> string(240) "{"CustomerID":1,"ImagePath":"C:\\DEV\\CustomerRewardsAdminPortal\\Resources\\Images\\headshot.jpg","ImageName":"headshot.jpg","ImageType":"image/jpg","ImageBlob":{"Headers":[{"Key":"Content-Type","Value":["image/jpg"]}]},"SourceImage":null}" } */ // reads the raw POST data $jsonPayload = filter_input_array(INPUT_POST); $input = json_decode($jsonPayload['MemberImageModel'], TRUE); //get the member data $this->imageModel->memberId = $input['CustomerID']; $this->imageModel->imagePath = $input['ImagePath']; $this->imageModel->imageName = $input['ImageName']; $this->imageModel->imageType = $input['ImageType']; //get the file data $this->imagePayload = $_FILES['image']; $this->imageModel->fileName = $this->imagePayload['name']; if(empty($this->imageModel->fileName)){ return $this->notFoundResponse(); } $this->imageModel->tempPath = $this->imagePayload['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 (!is_readable($this->imageModel->tempPath)) { return $this->notFoundResponse(); } $this->imageModel->imageBlob = base64_encode(file_get_contents($this->imageModel->tempPath)); 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->uploadTo . $this->imageModel->fileName)) { $moved = true; } return $moved; } private function transferImage() { $src = $this->imageModel->uploadTo; $dest = "/server/location/upload/" . $this->imageModel->fileName; $check = file_put_contents($dest, file_get_contents($src)); if($check != false){ $check = true; } /* Transfer between web servers if ( isset($_FILES['uploadedfile']) ) { $filename = $_FILES['uploadedfile']['tmp_name']; $handle = fopen($filename, "r"); $data = fread($handle, filesize($filename)); $POST_DATA = array( 'file' => base64_encode($data) ); $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, 'http://extserver.com/handle.php'); curl_setopt($curl, CURLOPT_TIMEOUT, 30); curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $POST_DATA); $response = curl_exec($curl); curl_close ($curl); } */ /* Separate file running on a different web server $encoded_file = $_POST['file']; $decoded_file = base64_decode($encoded_file); //Now you can copy the uploaded file to your server. file_put_contents('subins', $decoded_file); */ return $check; } private function validateImage(){ $validtion = false; if(in_array($this->imageModel->fileType, $this->imageModel->allowFileType)){ $validtion = true; } return $validtion; } }