Skip to content

Instantly share code, notes, and snippets.

@GeekyGeeky
Created January 19, 2021 21:16
Show Gist options
  • Save GeekyGeeky/feacdd60a042be1ab48eaaf1d127175c to your computer and use it in GitHub Desktop.
Save GeekyGeeky/feacdd60a042be1ab48eaaf1d127175c to your computer and use it in GitHub Desktop.
<?php
// required headers
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
header("Access-Control-Allow-Methods: POST");
header("Access-Control-Max-Age: 3600");
header("Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With");
include_once '../config/database.php';
include_once '../objects/freecourse.php';
$database = new Database();
$db = $database->getConnection();
$product = new Courses($db);
// $data = json_decode(file_get_contents("php://input"));
$title=isset($_POST['title']) ? $_POST['title'] : "";
$description=isset($_POST['description']) ? $_POST['description'] : "";
$category=isset($_POST['category']) ? $_POST['category'] : "";
$subcategory=isset($_POST['subcategory']) ? $_POST['subcategory'] : "";
$days=isset($_POST['days']) ? $_POST['days'] : "";
$starttime=isset($_POST['starttime']) ? $_POST['starttime'] : "";
$endtime=isset($_POST['endtime']) ? $_POST['endtime'] : "";
$aim=isset($_POST['aim']) ? $_POST['aim'] : "";
$requirements=isset($_POST['requirements']) ? $_POST['requirements'] : "";
$zoomlink=isset($_POST['zoomlink']) ? $_POST['zoomlink'] : "";
// decode jwt here
// if jwt is not empty
if($title and $description and $category and $subcategory and $days and $starttime and $endtime and $aim and $requirements and $zoomlink and $_FILES['image']){
$ds = DIRECTORY_SEPARATOR;
$ext = pathinfo($_FILES['image']['name'], PATHINFO_EXTENSION);
$storage_path = $_SERVER['DOCUMENT_ROOT'].$ds."imagez";
if (in_array(strtolower($ext), ['png', 'jpeg', 'jpg'])) {
$filename = time()."_".str_replace(" ", "_", $_FILES['image']['name']);
if (move_uploaded_file($_FILES['image']['tmp_name'], $storage_path.$ds.$file)) {
$product->title = $title;
$product->description = $description;
$product->category = $category;
$product->subcategory = $subcategory;
$product->days = $days;
$product->starttime = $starttime;
$product->endtime = $endtime;
$product->aim = $aim;
$product->requirements = $requirements;
$product->zoomlink = $zoomlink;
$product->created = date('l jS \of F Y h:i:s A');
$this->image = $filename;
// update image
if($product->create()){
http_response_code(201);
exit(json_encode(array("message" => "success", "product"=> $product)));
}else{
http_response_code(503);
exit(json_encode(array("message" => "Oooops!! Something went wrong.Try Again")));
}
// exit(json_encode(array("message" => "image added")));
}else{
http_response_code(503);
exit(json_encode(array("message" => "Unable to create product")));
}
}else{
http_response_code(400);
exit(json_encode(array("message" => "Image uploaded not allowed")));
}
}else{
http_response_code(422);
exit(json_encode(array("message" => "Oooops!! Something went wrong.Try Again")));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment