From a3bff0e30ec3777cf13203bafe1f1e76e98344f1 Mon Sep 17 00:00:00 2001 From: sctn4elk Date: Wed, 29 May 2024 12:27:49 -0500 Subject: [PATCH] Successful image upload to database --- Controller/API/ImageController.php | 15 +++++++------ Model/Database.php | 34 +++++++++++------------------- Model/ImageModel.php | 9 ++++---- 3 files changed, 25 insertions(+), 33 deletions(-) diff --git a/Controller/API/ImageController.php b/Controller/API/ImageController.php index 3522f97..28b9e6a 100644 --- a/Controller/API/ImageController.php +++ b/Controller/API/ImageController.php @@ -103,13 +103,8 @@ class ImageController extends BaseController { $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); + $this->imageModel->imageSize = filesize($this->imageModel->tempPath); - 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(); } @@ -118,13 +113,19 @@ class ImageController extends BaseController { return $this->notFoundResponse(); } + $memberImage = $this->imageModel->uploadTo . $this->imageModel->memberId; + if (!is_readable($memberImage)) { + return $this->notFoundResponse(); + } + $this->imageModel->imageBlob = file_get_contents($memberImage); + $response = $this->imageModel->insertImage(); return $response; } private function handleImage() { $moved = false; - if(move_uploaded_file($this->imageModel->tempPath, $this->imageModel->uploadTo . $this->imageModel->fileName)) { + if(move_uploaded_file($this->imageModel->tempPath, $this->imageModel->uploadTo . $this->imageModel->memberId)) { $moved = true; } return $moved; diff --git a/Model/Database.php b/Model/Database.php index d7a41d5..6ceff7a 100644 --- a/Model/Database.php +++ b/Model/Database.php @@ -70,27 +70,20 @@ class Database { return false; } - public function processImage($query = "") { + public function processImage($query = "", $imageModel) { try { if($this->connection == null) { $this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME); } - /*$stmt = $this->connection->prepare($query); - if($stmt === false) { - $msg = "Unable to prepare the statement: " . $query; - return $this->unprocessableQueryResponse($msg); - throw New Exception($msg); - }*/ - //return var_dump($query); - //$stmt->bind_param("ss", $params['filename'], $params['filepath']); - $result = $this->connection->execute_query($query); - //$result = $stmt->execute(); + + $data = $this->connection->real_escape_string($imageModel->imageBlob); + $result = $this->connection->query($query."(".$imageModel->memberId.", '".$imageModel->imageType."', '".$data."')"); + if($result === false) { $msg = "Unable to execute the statement: " . $query; return $this->unprocessableQueryResponse($msg); - throw New Exception($msg); } $rowCount = $this->connection->affected_rows; @@ -98,27 +91,24 @@ class Database { { $msg = "Statement did not return any rows: " . $query; return $this->unprocessableQueryResponse($msg); - throw New Exception($msg); } - - $stmt->close(); - return $rowCount; } catch(Exception $e) { - $msg = $e->getMessage(); + $msg = $query . " " . $e->getMessage(); return $this->unprocessableQueryResponse($msg); - throw New Exception($msg); } - return $this->unprocessableQueryResponse("Unknown error"); + + $this->connection->close(); + return $rowCount; } private function unprocessableQueryResponse($msg) { $response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Query'; $response['body'] = json_encode([ - 'error' => 'Invalid input' + 'error' => 'Invalid query' ]); - $this->strErrorDesc = 'Unprocessable Payload'; - $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload'; + $this->strErrorDesc = 'Unprocessable Query'; + $this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Query'; $this->strErrorMessage = $msg; return $response; } diff --git a/Model/ImageModel.php b/Model/ImageModel.php index 5f36289..cccc51f 100644 --- a/Model/ImageModel.php +++ b/Model/ImageModel.php @@ -23,6 +23,7 @@ class ImageModel extends Database { public $imagePath; public $imageName; public $imageType; + public $imageSize; public $imageBlob; public $uploadTo; @@ -43,7 +44,7 @@ class ImageModel extends Database { public function insertImage() { //$escaped_string = mysql_real_escape_string($this->imageBlob); - $escaped_string = addslashes($this->imageBlob); + //$escaped_string = addslashes($this->imageBlob); //return var_dump($escaped_string); //$sql = "INSERT INTO ImageStore(ImageId,Image) VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')"; @@ -62,11 +63,11 @@ class ImageModel extends Database { SET blob_col=LOAD_FILE('/tmp/picture') WHERE id=1; */ - $query = "CALL insert_loyalty_member_image_proc (" . $this->memberId . ", '" . $escaped_string . "');"; - + //$query = "CALL insert_loyalty_member_image_proc (" . $this->memberId . ", '" . $escaped_string . "');"; + $query = "CALL insert_loyalty_member_image_proc"; //return var_dump($query); - $rowCount = $this->processImage($query); + $rowCount = $this->processImage($query, $this); return $rowCount; } }