mirror of
https://github.com/sctn4elk/CustomerRewardsRESTAPI.git
synced 2025-01-10 09:24:31 -06:00
Successful image upload to database
This commit is contained in:
parent
9460fac030
commit
a3bff0e30e
|
@ -103,12 +103,7 @@ class ImageController extends BaseController {
|
||||||
$this->imageModel->basename = basename($this->imageModel->fileName);
|
$this->imageModel->basename = basename($this->imageModel->fileName);
|
||||||
$this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename;
|
$this->imageModel->originalPath = $this->imageModel->uploadTo.$this->imageModel->basename;
|
||||||
$this->imageModel->fileType = pathinfo($this->imageModel->originalPath, PATHINFO_EXTENSION);
|
$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()) {
|
if (! $this->validateImage()) {
|
||||||
return $this->unprocessableEntityResponse();
|
return $this->unprocessableEntityResponse();
|
||||||
|
@ -118,13 +113,19 @@ class ImageController extends BaseController {
|
||||||
return $this->notFoundResponse();
|
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();
|
$response = $this->imageModel->insertImage();
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
||||||
private function handleImage() {
|
private function handleImage() {
|
||||||
$moved = false;
|
$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;
|
$moved = true;
|
||||||
}
|
}
|
||||||
return $moved;
|
return $moved;
|
||||||
|
|
|
@ -70,27 +70,20 @@ class Database {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function processImage($query = "") {
|
public function processImage($query = "", $imageModel) {
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if($this->connection == null)
|
if($this->connection == null)
|
||||||
{
|
{
|
||||||
$this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME);
|
$this->connection = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE_NAME);
|
||||||
}
|
}
|
||||||
/*$stmt = $this->connection->prepare($query);
|
|
||||||
if($stmt === false) {
|
$data = $this->connection->real_escape_string($imageModel->imageBlob);
|
||||||
$msg = "Unable to prepare the statement: " . $query;
|
$result = $this->connection->query($query."(".$imageModel->memberId.", '".$imageModel->imageType."', '".$data."')");
|
||||||
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();
|
|
||||||
if($result === false) {
|
if($result === false) {
|
||||||
$msg = "Unable to execute the statement: " . $query;
|
$msg = "Unable to execute the statement: " . $query;
|
||||||
return $this->unprocessableQueryResponse($msg);
|
return $this->unprocessableQueryResponse($msg);
|
||||||
throw New Exception($msg);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$rowCount = $this->connection->affected_rows;
|
$rowCount = $this->connection->affected_rows;
|
||||||
|
@ -98,27 +91,24 @@ class Database {
|
||||||
{
|
{
|
||||||
$msg = "Statement did not return any rows: " . $query;
|
$msg = "Statement did not return any rows: " . $query;
|
||||||
return $this->unprocessableQueryResponse($msg);
|
return $this->unprocessableQueryResponse($msg);
|
||||||
throw New Exception($msg);
|
}
|
||||||
|
} catch(Exception $e) {
|
||||||
|
$msg = $query . " " . $e->getMessage();
|
||||||
|
return $this->unprocessableQueryResponse($msg);
|
||||||
}
|
}
|
||||||
|
|
||||||
$stmt->close();
|
$this->connection->close();
|
||||||
return $rowCount;
|
return $rowCount;
|
||||||
} catch(Exception $e) {
|
|
||||||
$msg = $e->getMessage();
|
|
||||||
return $this->unprocessableQueryResponse($msg);
|
|
||||||
throw New Exception($msg);
|
|
||||||
}
|
|
||||||
return $this->unprocessableQueryResponse("Unknown error");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private function unprocessableQueryResponse($msg)
|
private function unprocessableQueryResponse($msg)
|
||||||
{
|
{
|
||||||
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Query';
|
$response['status_code_header'] = 'HTTP/1.1 422 Unprocessable Query';
|
||||||
$response['body'] = json_encode([
|
$response['body'] = json_encode([
|
||||||
'error' => 'Invalid input'
|
'error' => 'Invalid query'
|
||||||
]);
|
]);
|
||||||
$this->strErrorDesc = 'Unprocessable Payload';
|
$this->strErrorDesc = 'Unprocessable Query';
|
||||||
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Payload';
|
$this->strErrorHeader = 'HTTP/1.1 422 Unprocessable Query';
|
||||||
$this->strErrorMessage = $msg;
|
$this->strErrorMessage = $msg;
|
||||||
return $response;
|
return $response;
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,6 +23,7 @@ class ImageModel extends Database {
|
||||||
public $imagePath;
|
public $imagePath;
|
||||||
public $imageName;
|
public $imageName;
|
||||||
public $imageType;
|
public $imageType;
|
||||||
|
public $imageSize;
|
||||||
public $imageBlob;
|
public $imageBlob;
|
||||||
|
|
||||||
public $uploadTo;
|
public $uploadTo;
|
||||||
|
@ -43,7 +44,7 @@ class ImageModel extends Database {
|
||||||
public function insertImage()
|
public function insertImage()
|
||||||
{
|
{
|
||||||
//$escaped_string = mysql_real_escape_string($this->imageBlob);
|
//$escaped_string = mysql_real_escape_string($this->imageBlob);
|
||||||
$escaped_string = addslashes($this->imageBlob);
|
//$escaped_string = addslashes($this->imageBlob);
|
||||||
//return var_dump($escaped_string);
|
//return var_dump($escaped_string);
|
||||||
//$sql = "INSERT INTO ImageStore(ImageId,Image) VALUES('$this->image_id','" . mysql_escape_string(file_get_contents($tmp_image)) . "')";
|
//$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')
|
SET blob_col=LOAD_FILE('/tmp/picture')
|
||||||
WHERE id=1;
|
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);
|
//return var_dump($query);
|
||||||
|
|
||||||
$rowCount = $this->processImage($query);
|
$rowCount = $this->processImage($query, $this);
|
||||||
return $rowCount;
|
return $rowCount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user