revert.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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'] === "DELETE")
  15. {
  16. $uniqueFileID = $_GET["key"];
  17. function revertImagesFromUploadsLocation()
  18. {
  19. global $uniqueFileID;
  20. $imgName = null;
  21. // check if there is a filename in the DB with key and campaignId
  22. $arrayDBStore = readJsonFile();
  23. $imageInfoIndex = array_search($uniqueFileID, array_column($arrayDBStore, 'id'));
  24. if (isset($imageInfoIndex))
  25. {
  26. $imageInfo = $arrayDBStore[$imageInfoIndex];
  27. $imgName = $imageInfo["name"];
  28. }
  29. // check if there is file ($imgName) in ./images/ path on the server
  30. $imgFilePointer = UPLOAD_DIR . $imgName;
  31. // if file exists --> delete file from server
  32. if (file_exists($imgFilePointer))
  33. {
  34. $filedeleted = unlink($imgFilePointer);
  35. if ($filedeleted)
  36. {
  37. // removing file from DB as well
  38. unset($arrayDBStore[$imageInfoIndex]);
  39. writeJsonFile(array_values($arrayDBStore));
  40. }
  41. return $filedeleted;
  42. }
  43. else
  44. {
  45. return true;
  46. }
  47. }
  48. $response = [];
  49. // trigger revertFunction
  50. if (revertImagesFromUploadsLocation())
  51. {
  52. $response["status"] = "success";
  53. $response["key"] = $uniqueFileID;
  54. http_response_code(200);
  55. }
  56. else
  57. {
  58. $response["status"] = "error";
  59. $response["msg"] = "File could not be deleted";
  60. http_response_code(400);
  61. }
  62. header('Content-Type: application/json');
  63. echo json_encode($response);
  64. exit();
  65. }
  66. else
  67. {
  68. exit();
  69. }
  70. ?>