upload.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <?php
  2. ini_set('display_errors', 1);
  3. ini_set('display_startup_errors', 1);
  4. error_reporting(E_ALL);
  5. if($_SERVER["REQUEST_METHOD"] == "OPTIONS")
  6. {
  7. if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_METHOD"]))
  8. header("Access-Control-Allow-Methods: POST, GET, OPTIONS, DELETE, PUT"); //Make sure you remove those you do not want to support
  9. header("Access-Control-Allow-Origin: *");
  10. if (isset($_SERVER["HTTP_ACCESS_CONTROL_REQUEST_HEADERS"]))
  11. header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
  12. //Just exit with 200 OK with the above headers for OPTIONS method
  13. exit(0);
  14. }
  15. if($_POST) {
  16. $target_dir = "./uploads/";
  17. $target_file = $target_dir . basename($_FILES["files"]["name"][0]);
  18. if (move_uploaded_file($_FILES["files"]["tmp_name"][0], $target_file)) {
  19. header("Access-Control-Allow-Origin: *");
  20. header('Content-type: application/json');
  21. $data = ["url" => $target_file, "message" => "The file ". basename( $_FILES["files"]["name"][0]). " has been uploaded."];
  22. http_response_code(201);
  23. echo json_encode( $data );
  24. } else {
  25. header("Access-Control-Allow-Origin: *");
  26. header('Content-type: application/json');
  27. $data = ["message" => "Sorry, there was an error uploading your file."];
  28. http_response_code(400);
  29. echo json_encode( $data );
  30. }
  31. }?>