$target_file, 'message' => 'The file ' . basename($_FILES['file']['name']) . ' has been uploaded.']; http_response_code(201); echo json_encode($data); } else { throw new Exception('Unable to move the uploaded file to its final location:' . $target_file); } } catch (\Throwable $th) { header('Access-Control-Allow-Origin: *'); header('Content-type: application/json'); $data = ['message' => 'Sorry, there was an error uploading your file.', 'error' => $th->getMessage()]; http_response_code(400); echo json_encode($data); } }