process.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. // Comment if you don't want to allow posts from other domains
  3. header('Access-Control-Allow-Origin: *');
  4. // Allow the following methods to access this file
  5. header('Access-Control-Allow-Methods: OPTIONS, GET, DELETE, POST, HEAD, PATCH');
  6. // Allow the following headers in preflight
  7. header('Access-Control-Allow-Headers: content-type, upload-length, upload-offset, upload-name');
  8. // Allow the following headers in response
  9. header('Access-Control-Expose-Headers: upload-offset');
  10. // Load our configuration for this server
  11. require_once ('config.php');
  12. require ("./util/upload_media.php");
  13. require ("./util/read_write_functions.php");
  14. if ($_SERVER['REQUEST_METHOD'] === 'POST')
  15. {
  16. $files = $_FILES["filepond"];
  17. $imageName = null;
  18. $id = null;
  19. function saveImagesToTempLocation($uploadedFile)
  20. {
  21. global $imageName;
  22. global $id;
  23. $imageUniqueId = null;
  24. // check that there were no errors while uploading file
  25. if (isset($uploadedFile) && $uploadedFile['error'] === UPLOAD_ERR_OK)
  26. {
  27. $imageName = uploadImage($uploadedFile, UPLOAD_DIR);
  28. if ($imageName)
  29. {
  30. $filePointer = UPLOAD_DIR . $imageName;
  31. $arrayDBStore = readJsonFile();
  32. $id = uniqid();
  33. $newImageInfo = ["id" => $id, "name" => $imageName, "date" => time() ];
  34. array_push($arrayDBStore, $newImageInfo);
  35. writeJsonFile($arrayDBStore);
  36. }
  37. }
  38. return $id;
  39. }
  40. $structuredFiles = [];
  41. if (isset($files))
  42. {
  43. foreach ($files["name"] as $filename)
  44. {
  45. $structuredFiles[] = ["name" => $filename];
  46. }
  47. foreach ($files["type"] as $index => $filetype)
  48. {
  49. $structuredFiles[$index]["type"] = $filetype;
  50. }
  51. foreach ($files["tmp_name"] as $index => $file_tmp_name)
  52. {
  53. $structuredFiles[$index]["tmp_name"] = $file_tmp_name;
  54. }
  55. foreach ($files["error"] as $index => $file_error)
  56. {
  57. $structuredFiles[$index]["error"] = $file_error;
  58. }
  59. foreach ($files["size"] as $index => $file_size)
  60. {
  61. $structuredFiles[$index]["size"] = $file_size;
  62. }
  63. }
  64. $uniqueImgID = null;
  65. if (count($structuredFiles))
  66. {
  67. foreach ($structuredFiles as $structuredFile)
  68. {
  69. $uniqueImgID = saveImagesToTempLocation($structuredFile);
  70. }
  71. }
  72. $response = [];
  73. if ($uniqueImgID)
  74. {
  75. $response["status"] = "success";
  76. $response["key"] = $uniqueImgID;
  77. $response["msg"] = null;
  78. $response["files"] = json_encode($structuredFiles);
  79. http_response_code(200);
  80. }
  81. else
  82. {
  83. $response["status"] = "error";
  84. $response["key"] = null;
  85. $response["msg"] = "An error occured while uploading image";
  86. $response["files"] = json_encode($structuredFiles);
  87. http_response_code(400);
  88. }
  89. header('Content-Type: application/json');
  90. echo json_encode($response);
  91. exit();
  92. }
  93. else
  94. {
  95. exit();
  96. }
  97. ?>