upload.php 573 B

12345678910111213141516171819202122
  1. <?php
  2. header('Content-Type: application/json');
  3. if ($_SERVER['REQUEST_METHOD'] !== 'POST') {
  4. http_response_code(405);
  5. return json_encode(['error' => 'Method not allowed']);
  6. }
  7. if (!isset($_FILES['file'])) {
  8. http_response_code(400);
  9. return json_encode(['error' => 'No file uploaded']);
  10. }
  11. $uploadedImage = uploadImage($_FILES['file']);
  12. if ($uploadedImage) {
  13. return json_encode(['location' => "/$uploadedImage"]);
  14. } else {
  15. http_response_code(400);
  16. return json_encode(['error' => 'Failed to upload image. Please check file format and size.']);
  17. }