Complete image upload work on storing image to database

This commit is contained in:
sctn4elk 2024-05-15 14:54:33 -05:00
parent cf1fb1bc63
commit b877d2aaea
8 changed files with 23 additions and 36 deletions

View File

@ -1,12 +1,11 @@
<?php <?php
header("Content-Type: application/json"); header("Content-Type: application/json");
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Model/CustomerModel.php"; require_once PD . "/Model/CustomerModel.php";
/** /**
* Description of CustomerController * Description of CustomerController
* *
* @author SCTN4 * @author Mike Howard
* http://localhost:8000/index.php/customer/process/insert?name=Mike%20Howard&email=sctn4elk@msn.com&phone=208-841-4159&birthday=05/07/1965&loyalty=1&city=Winnsboro&state=TX&zip=75494 * http://localhost:8000/index.php/customer/process/insert?name=Mike%20Howard&email=sctn4elk@msn.com&phone=208-841-4159&birthday=05/07/1965&loyalty=1&city=Winnsboro&state=TX&zip=75494
* http://localhost:8000/index.php/customer/process/select?limit=20 * http://localhost:8000/index.php/customer/process/select?limit=20
*/ */
@ -28,7 +27,7 @@ class CustomerController extends BaseController {
try { try {
$this->requestMethod = $this->getServerRequestMethod(); $this->requestMethod = $this->getServerRequestMethod();
//return var_dump($this->requestMethod);
switch($this->action) { switch($this->action) {
case "select": case "select":
$response = $this->selectAction(); $response = $this->selectAction();
@ -80,7 +79,6 @@ class CustomerController extends BaseController {
} }
$this->customerModel = new CustomerModel(); $this->customerModel = new CustomerModel();
//return var_dump($this->customerModel);
$this->arrQueryStringParams = $this->getQueryStringParams(); $this->arrQueryStringParams = $this->getQueryStringParams();
if (isset($this->arrQueryStringParams['customer_id'])) { if (isset($this->arrQueryStringParams['customer_id'])) {

View File

@ -1,15 +1,15 @@
<?php <?php
header("Content-Type: application/json"); header("Content-Type: multipart/form-data");
header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Methods: POST"); //header("Access-Control-Allow-Methods: POST");
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI'); //define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Model/ImageModel.php"; require_once PD . "/Model/ImageModel.php";
/** /**
* Description of ImageController * Description of ImageController
* *
* @author SCTN4 * @author Mike Howard
*/ */
class ImageController extends BaseController { class ImageController extends BaseController {
@ -77,13 +77,14 @@ class ImageController extends BaseController {
return $this->unprocessableRequestResponse(); return $this->unprocessableRequestResponse();
} }
$this->imagePayload = file_get_contents($_FILES['image']['tmp_name']); $this->imagePayload = $_FILES['image'];
$this->imageModel->fileName = $_FILES['image']['name'];
$this->imageModel->fileName = $this->imagePayload['name'];
if(empty($this->imageModel->fileName)){ if(empty($this->imageModel->fileName)){
return $this->notFoundResponse(); return $this->notFoundResponse();
} }
$this->imageModel->tempPath = $_FILES["image"]["tmp_name"]; $this->imageModel->tempPath = $this->imagePayload['tmp_name'];
$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);
@ -91,7 +92,7 @@ class ImageController extends BaseController {
if (! $this->validateImage()) { if (! $this->validateImage()) {
return $this->unprocessableEntityResponse(); return $this->unprocessableEntityResponse();
} }
if (! $this->handleImage()) { if (! $this->handleImage()) {
return $this->notFoundResponse(); return $this->notFoundResponse();
} }
@ -102,7 +103,7 @@ class ImageController extends BaseController {
private function handleImage() { private function handleImage() {
$moved = false; $moved = false;
if(!move_uploaded_file($this->imageModel->tempPath, $this->imageModel->originalPath)) { if(!move_uploaded_file($_FILES['image']['tmp_name'], $this->imageModel->uploadTo . $this->imageModel->fileName)) {
$moved = true; $moved = true;
} }
return $moved; return $moved;

View File

@ -6,6 +6,7 @@
*/ */
define("PROJECT_ROOT_PATH", __DIR__ . "/../"); define("PROJECT_ROOT_PATH", __DIR__ . "/../");
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
// include main configuration file // include main configuration file
require_once PROJECT_ROOT_PATH . "/include/config.php"; require_once PROJECT_ROOT_PATH . "/include/config.php";
// include the base controller file // include the base controller file

View File

@ -1,4 +1,9 @@
<?php <?php
//define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
//require_once PROJECT_ROOT_PATH . "/Model/Database.php";
require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php";
/** /**
* Description of CustomerModel * Description of CustomerModel
* *
@ -22,11 +27,6 @@
* address_zip varchar(10) * address_zip varchar(10)
*/ */
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
//require_once PROJECT_ROOT_PATH . "/Model/Database.php";
require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php";
class CustomerModel extends Database { class CustomerModel extends Database {
use ModelTraits; use ModelTraits;

View File

@ -46,7 +46,6 @@ class Database {
{ {
try { try {
//Prepare the statement //Prepare the statement
//$stmt = $this->connection->stmt_init();
$stmt = $this->connection->prepare($query); $stmt = $this->connection->prepare($query);
if($stmt === false) { if($stmt === false) {
throw New Exception("Unable to prepare the statement: " . $query); throw New Exception("Unable to prepare the statement: " . $query);

View File

@ -1,4 +1,6 @@
<?php <?php
require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php";
/** /**
* Description of ImageModel * Description of ImageModel
@ -14,10 +16,6 @@
* loyalty_value_blob blob * loyalty_value_blob blob
*/ */
define('PD', 'D:\DEV\Git Repository\CustomerRewardsRESTAPI');
require_once PD . "/Model/Database.php";
require_once PD . "/Model/ModelTraits.php";
class ImageModel extends Database { class ImageModel extends Database {
use ModelTraits; use ModelTraits;
@ -30,17 +28,13 @@ class ImageModel extends Database {
public $fileType; public $fileType;
function __construct() { function __construct() {
$this->uploadTo = "public/images/"; $this->uploadTo = PD . "/public/images/";
$this->allowFileType = array('jpg','png','jpeg','gif'); $this->allowFileType = array('jpg','png','jpeg','gif');
} }
/* Process the uploaded image and store in database */ /* Process the uploaded image and store in database */
public function insertImage() public function insertImage()
{ {
//return var_dump($jsonPayLoad);
$keys = array_keys($inputModel);
$n = count($keys);
//$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)) . "')";
$query .= "CALL insert_loyalty_member_image_proc (" . $inputModel[$keys[0]] . ", "; $query .= "CALL insert_loyalty_member_image_proc (" . $inputModel[$keys[0]] . ", ";

View File

@ -3,7 +3,7 @@
/** /**
* Description of ModelTraits * Description of ModelTraits
* *
* @author SCTN4 * @author Mike Howard
*/ */
trait ModelTraits { trait ModelTraits {
private $params = array(); private $params = array();

View File

@ -1,14 +1,9 @@
<?php <?php
header("Content-Type: application/json");header("Access-Control-Allow-Origin: *"); header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE"); header("Access-Control-Allow-Methods: OPTIONS,GET,POST,PUT,DELETE");
header("Access-Control-Max-Age: 3600"); header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With"); header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Scripting/EmptyPHP.php to edit this template
*/
/* USAGE /* USAGE
* https://localhost/index.php/{MODULE_NAME}/{METHOD_NAME}?limit={LIMIT_VALUE} * https://localhost/index.php/{MODULE_NAME}/{METHOD_NAME}?limit={LIMIT_VALUE}
* http://localhost/index.php/customer/process/list?limit=20 * http://localhost/index.php/customer/process/list?limit=20
@ -41,7 +36,6 @@ switch($uri[$uri_pos + 1]) {
case "purchase": case "purchase":
header("HTTP/1.1 404 Module Not Defined"); header("HTTP/1.1 404 Module Not Defined");
exit(); exit();
//require PROJECT_ROOT_PATH . "/Controller/Api/PurchaseController.php";
//$objFeedController = new PurchaseController(); //$objFeedController = new PurchaseController();
break; break;