load.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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'] === "GET")
  15. {
  16. function getRequestHeaders()
  17. {
  18. $headers = array();
  19. foreach ($_SERVER as $key => $value)
  20. {
  21. if (substr($key, 0, 5) <> 'HTTP_')
  22. {
  23. continue;
  24. }
  25. $header = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
  26. $headers[$header] = $value;
  27. }
  28. return $headers;
  29. }
  30. // $headers = getRequestHeaders();
  31. // header('Content-Type: application/json');
  32. // echo json_encode(["headers" =>$headers, "get" => $_GET]);
  33. // exit();
  34. $uniqueFileID = $_GET["key"];
  35. function loadLocalImage()
  36. {
  37. global $uniqueFileID;
  38. $imageName = null;
  39. // checking if image exists in db with uniqueFileID
  40. $arrayDBStore = readJsonFile();
  41. $imageInfoIndex = array_search($uniqueFileID, array_column($arrayDBStore, 'id'));
  42. if (isset($imageInfoIndex))
  43. {
  44. $imageInfo = $arrayDBStore[$imageInfoIndex];
  45. $imageName = $imageInfo["name"];
  46. }
  47. // if imageName was found in the DB, get file with imageName and return file object or blob
  48. $imagePointer = UPLOAD_DIR . $imageName;
  49. $fileObject = null;
  50. if ($imageName && file_exists($imagePointer))
  51. {
  52. $fileObject = file_get_contents($imagePointer);
  53. }
  54. return [$fileObject, $imageName];
  55. }
  56. // trigger load local image
  57. $loadImageResultArr = [$fileBlob, $imageName] = loadLocalImage();
  58. if ($fileBlob)
  59. {
  60. $imagePointer = UPLOAD_DIR . $imageName;
  61. $fileContextType = mime_content_type($imagePointer);
  62. $fileSize = filesize($imagePointer);
  63. // $handle = fopen($imagePointer, 'r');
  64. // if (!$handle) return false;
  65. // $content = fread($handle, filesize($imagePointer));
  66. http_response_code(200);
  67. header('Access-Control-Expose-Headers: Content-Disposition, Content-Length, X-Content-Transfer-Id');
  68. header("Content-Type: $fileContextType");
  69. header("Content-Length: $fileSize");
  70. header("Content-Disposition: inline; filename='$imageName'");
  71. echo $fileBlob;
  72. // echo json_encode(strlen($fileBlob));
  73. }
  74. else
  75. {
  76. http_response_code(500);
  77. }
  78. exit();
  79. }
  80. else
  81. {
  82. http_response_code(400);
  83. exit();
  84. }
  85. ?>