Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- header("content-type:application/json");
- require_once __DIR__ . '../../db_config.php';
- function addEvent($userID, $title, $desc, $category, $venue, $date, $time) {
- $imageResponse = storeImage();
- if($imageResponse['upload']) {
- $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysql_error());
- $date = new DateTime($date, new DateTimeZone('Pacific/Nauru'));
- $date = $date->format('Y-m-d');
- $datetime = $date . " " . $time;
- $filename = $imageResponse['filename'];
- $myquery = "INSERT INTO `Timeline` (`UserID`, `Title`, `Description`, `Category`, `Venue`, `Image`, `DateTime`) ";
- $myquery .= "VALUES ($userID, '$title', '$desc', $category, '$venue', '$filename', '$datetime') ";
- $result = mysqli_query($con, $myquery);
- $response["status"] = "";
- if($result) {
- $response["status"] = "success";
- }
- else {
- $response["status"] = "failed";
- }
- return $response;
- }
- else {
- $response["status"] = $imageResponse['error'];
- }
- }
- function storeImage() {
- $response = array ("upload" => false, "error" => NULL, "filename" => NULL);
- $target_dir = "../img/timeline/";
- $temp = explode(".", $_FILES["image"]["name"]);
- $newfilename = round(microtime(true)) . '.' . end($temp);
- $response["filename"] = $newfilename;
- $target_file = $target_dir . $newfilename;
- $uploadOk = 1;
- $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
- // Check if image file is a actual image or fake image
- if(isset($_POST["submit"])) {
- $check = getimagesize($_FILES["image"]["tmp_name"]);
- if($check !== false) {
- echo "File is an image - " . $check["mime"] . ".";
- $uploadOk = 1;
- }
- else {
- echo "File is not an image.";
- $uploadOk = 0;
- }
- }
- // Check if file already exists
- if (file_exists($target_file)) {
- $response["error"] = "Sorry, file already exists.";
- $uploadOk = 0;
- }
- // Check file size
- if ($_FILES["image"]["size"] > 500000) {
- $response["error"] = "Sorry, your file is too large.";
- $uploadOk = 0;
- }
- // Allow certain file formats
- if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
- $response["error"] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
- $uploadOk = 0;
- }
- // Check if $uploadOk is set to 0 by an error
- if ($uploadOk == 0) {
- $response["error"] = "Sorry, your file was not uploaded.";
- // if everything is ok, try to upload file
- }
- else {
- if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
- $response["upload"] = true;
- }
- else {
- $response["error"] = "Sorry, there was an error uploading your file.";
- }
- }
- return $response;
- }
- if(isset($_POST['title']) && isset($_POST['description']) && isset($_POST['category']) && isset($_POST['place']) && isset($_POST['date']) && isset($_POST['time']) && isset($_FILES["image"]["name"])) {
- echo json_encode(addEvent(1, $_POST['title'], $_POST['description'], $_POST['category'], $_POST['place'], $_POST['date'], $_POST['time']));
- }
- else {
- $response["status"] = "invalid paramater";
- echo json_encode($response);
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement