FileHandler.php
====================
<?php
class FileHandler
{
private $con;
public function __construct()
{
require_once dirname(__FILE__) . '/DbConnect.php';
$db = new DbConnect();
$this->con = $db->connect();
}
public function saveFile($file, $extension, $desc)
{
$name = round(microtime(true) * 1000) . '.' . $extension;
$filedest = dirname(__FILE__) . UPLOAD_PATH . $name;
move_uploaded_file($file, $filedest);
$url = $server_ip = gethostbyname(gethostname());
$stmt = $this->con->prepare("INSERT INTO images (description, image) VALUES (?, ?)");
$stmt->bind_param("ss", $desc, $name);
if ($stmt->execute()){
return true;
}
else{
//error !! don't go further
var_dump($this->db->error);
}
return false;
}
public function saveFil($file, $extension, $sname, $phone)
{
$name = round(microtime(true) * 1000) . '.' . $extension;
$filedest = dirname(__FILE__) . UPLOAD_PATH . $name;
move_uploaded_file($file, $filedest);
$url = $server_ip = gethostbyname(gethostname());
$stmt = $this->con->prepare("INSERT INTO image(sname,image,phone) VALUES (?, ?, ?)");
$stmt->bind_param("sss", $sname, $name,$phone);
if ($stmt->execute()){
return true;
}
else{
//error !! don't go further
//secho $this->db->error;
}
return false;
}
//$sname,$email, $phone,$roll,$session,$year,$homedst
public function saveCSE($file, $extension, $sname, $email, $phone,$roll,$year,$homedst)
{
$name = round(microtime(true) * 1000) . '.' . $extension;
$filedest = dirname(__FILE__) . UPLOAD_PATH . $name;
move_uploaded_file($file, $filedest);
$url = $server_ip = gethostbyname(gethostname());
$stmt = $this->con->prepare("INSERT INTO csestudent(sname,image,email, phone,roll,year,homedst) VALUES (?, ?, ?, ?, ?, ?, ?)");
$stmt->bind_param("sssssss", $sname,$name,$email, $phone,$roll,$year,$homedst);
if ($stmt->execute()){
return true;
}
else{
//error !! don't go further
//echo $this->db->error;
}
return false;
}
public function get($phone, $roll)
{
$stmt = $this->con->prepare("SELECT phone, roll, image FROM csestudent where phone= $phone || roll= $roll");
$stmt->execute();
$stmt->bind_result($phone,$roll,$image);
$images = array();
while ($stmt->fetch()) {
$temp = array();
$absurl = 'http://' . gethostbyname(gethostname()) . '/ImageUploadApi' . UPLOAD_PATH . $image;
$temp['phone'] = $phone;
$temp['roll'] = $roll;
$temp['image'] = $absurl;
array_push($images, $temp);
}
return $images;
}
public function getAllFiles()
{
$stmt = $this->con->prepare("SELECT id, description, image FROM images ORDER BY id DESC");
$stmt->execute();
$stmt->bind_result($id, $desc, $image);
$images = array();
while ($stmt->fetch()) {
$temp = array();
$absurl = 'http://' . gethostbyname(gethostname()) . '/ImageUploadApi' . UPLOAD_PATH . $image;
$temp['id'] = $id;
$temp['desc'] = $desc;
$temp['image'] = $absurl;
array_push($images, $temp);
}
return $images;
}
}
?>
Api.php
==================
<?php
require_once dirname(__FILE__) . '/FileHandler.php';
$response = array();
if (isset($_GET['apicall'])) {
switch ($_GET['apicall']) {
case 'upload':
if (isset($_POST['desc']) && strlen($_POST['desc']) > 0 && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$upload = new FileHandler();
$file = $_FILES['image']['tmp_name'];
$desc = $_POST['desc'];
if ($upload->saveFile($file, getFileExtension($_FILES['image']['name']), $desc)) {
$response['error'] = false;
$response['message'] = 'File Uploaded Successfullly';
}
} else {
$response['error'] = true;
$response['message'] = 'Required parameters are not available';
}
break;
case 'uploadimage':
if (isset($_POST['sname']) && strlen($_POST['sname']) > 0 && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$upload = new FileHandler();
$file = $_FILES['image']['tmp_name'];
$sname = $_POST['sname'];
$phone = $_POST['phone'];
if ($upload->saveFil($file, getFileExtension($_FILES['image']['name']), $sname, $phone)) {
$response['error'] = false;
$response['message'] = 'File Uploaded Successfullly';
}
} else {
$response['error'] = true;
$response['message'] = 'Required parameters are not available';
}
break;
case 'csestu':
//$email, $phone,$roll,$session,$year,$homedst
if (isset($_POST['sname']) && strlen($_POST['sname']) > 0 && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
$upload = new FileHandler();
$file = $_FILES['image']['tmp_name'];
$sname = $_POST['sname'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$roll = $_POST['roll'];
$year = $_POST['year'];
$homedst = $_POST['homedst'];
$response['check'] = $email.$phone;
if ($upload->saveCSE($file, getFileExtension($_FILES['image']['name']), $sname, $email, $phone,$roll,$year,$homedst)) {
$response['error'] = false;
$response['message'] = 'File Uploaded Successfullly';
}
} else {
$response['error'] = true;
$response['message'] = 'Required parameters are not available';
}
break;
//get info from server
case 'checkinfo':
// , $phone,$roll
if (isset($_POST['phone']) && strlen($_POST['phone']) > 0) {
$upload = new FileHandler();
$phone = $_POST['phone'];
$roll = $_POST['roll'];
$response['error'] = false;
$response['message'] = $upload->get($phone,$roll);
}
break;
case 'getallimages':
$upload = new FileHandler();
$response['error'] = false;
$response['images'] = $upload->getAllFiles();
break;
default:
$response['error'] = true;
$response['message'] = 'sory are not available';
break;
}
}
echo json_encode($response);
function getFileExtension($file)
{
$path_parts = pathinfo($file);
return $path_parts['extension'];
}
?>
====================0===================
================
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASS', '');
define('DB_NAME', 'imageupload');
define('UPLOAD_PATH', '/uploads/');
?>
DbConnect.php
===========
<?php
class DbConnect
{
private $con;
public function connect()
{
require_once dirname(__FILE__) . '/Constants.php';
$this->con = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
if (mysqli_connect_errno()) {
echo 'Failed to connect ' . mysqli_connect_error();
return null;
}
return $this->con;
}
}
?>