Successful image upload to database

This commit is contained in:
sctn4elk 2024-05-29 12:27:49 -05:00
parent 9460fac030
commit a3bff0e30e
3 changed files with 25 additions and 33 deletions

View File

@ -103,12 +103,7 @@ 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);
if (!is_readable($this->imageModel->tempPath)) {
return $this->notFoundResponse();
}
$this->imageModel->imageBlob = base64_encode(file_get_contents($this->imageModel->tempPath));
$this->imageModel->imageSize = filesize($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;

View File

@ -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;
}

View File

@ -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;
}
}