Advertisement
Guest User

Untitled

a guest
Feb 27th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.67 KB | None | 0 0
  1. <?php
  2. header("content-type:application/json");
  3. require_once __DIR__ . '../../db_config.php';
  4. function addEvent($userID, $title, $desc, $category, $venue, $date, $time) {
  5. $imageResponse = storeImage();
  6. if($imageResponse['upload']) {
  7. $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD, DB_DATABASE) or die(mysql_error());
  8. $date = new DateTime($date, new DateTimeZone('Pacific/Nauru'));
  9. $date = $date->format('Y-m-d');
  10. $datetime = $date . " " . $time;
  11. $filename = $imageResponse['filename'];
  12. $myquery = "INSERT INTO `Timeline` (`UserID`, `Title`, `Description`, `Category`, `Venue`, `Image`, `DateTime`) ";
  13. $myquery .= "VALUES ($userID, '$title', '$desc', $category, '$venue', '$filename', '$datetime') ";
  14. $result = mysqli_query($con, $myquery);
  15. $response["status"] = "";
  16. if($result) {
  17. $response["status"] = "success";
  18. }
  19. else {
  20. $response["status"] = "failed";
  21. }
  22. return $response;
  23. }
  24. else {
  25. $response["status"] = $imageResponse['error'];
  26. }
  27. }
  28. function storeImage() {
  29. $response = array ("upload" => false, "error" => NULL, "filename" => NULL);
  30. $target_dir = "../img/timeline/";
  31. $temp = explode(".", $_FILES["image"]["name"]);
  32. $newfilename = round(microtime(true)) . '.' . end($temp);
  33. $response["filename"] = $newfilename;
  34. $target_file = $target_dir . $newfilename;
  35. $uploadOk = 1;
  36. $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
  37. // Check if image file is a actual image or fake image
  38. if(isset($_POST["submit"])) {
  39. $check = getimagesize($_FILES["image"]["tmp_name"]);
  40. if($check !== false) {
  41. echo "File is an image - " . $check["mime"] . ".";
  42. $uploadOk = 1;
  43. }
  44. else {
  45. echo "File is not an image.";
  46. $uploadOk = 0;
  47. }
  48. }
  49. // Check if file already exists
  50. if (file_exists($target_file)) {
  51. $response["error"] = "Sorry, file already exists.";
  52. $uploadOk = 0;
  53. }
  54. // Check file size
  55. if ($_FILES["image"]["size"] > 500000) {
  56. $response["error"] = "Sorry, your file is too large.";
  57. $uploadOk = 0;
  58. }
  59. // Allow certain file formats
  60. if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg" && $imageFileType != "gif" ) {
  61. $response["error"] = "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
  62. $uploadOk = 0;
  63. }
  64. // Check if $uploadOk is set to 0 by an error
  65. if ($uploadOk == 0) {
  66. $response["error"] = "Sorry, your file was not uploaded.";
  67. // if everything is ok, try to upload file
  68. }
  69. else {
  70. if (move_uploaded_file($_FILES["image"]["tmp_name"], $target_file)) {
  71. $response["upload"] = true;
  72. }
  73. else {
  74. $response["error"] = "Sorry, there was an error uploading your file.";
  75. }
  76. }
  77. return $response;
  78. }
  79. if(isset($_POST['title']) && isset($_POST['description']) && isset($_POST['category']) && isset($_POST['place']) && isset($_POST['date']) && isset($_POST['time']) && isset($_FILES["image"]["name"])) {
  80. echo json_encode(addEvent(1, $_POST['title'], $_POST['description'], $_POST['category'], $_POST['place'], $_POST['date'], $_POST['time']));
  81. }
  82. else {
  83. $response["status"] = "invalid paramater";
  84. echo json_encode($response);
  85. }
  86. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement