diff options
| author | ilotterytea <iltsu@alright.party> | 2025-03-18 02:43:15 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-03-18 02:43:15 +0500 |
| commit | eacbea32efb110363e461c344100ca9511a97630 (patch) | |
| tree | e74d86289698ebb376a199251803f61a9cab70d2 | |
| parent | ad1f0a370d432701bc5fe4eb6fa1fb23f94c1293 (diff) | |
upd: json responses
| -rw-r--r-- | lib/utils.php | 11 | ||||
| -rw-r--r-- | public/upload.php | 17 |
2 files changed, 21 insertions, 7 deletions
diff --git a/lib/utils.php b/lib/utils.php new file mode 100644 index 0000000..b5f8431 --- /dev/null +++ b/lib/utils.php @@ -0,0 +1,11 @@ +<?php +function json_response(mixed $data, string|null $message, int $code = 200) +{ + http_response_code($code); + header('Content-Type: application/json'); + echo json_encode([ + 'status_code' => $code, + 'message' => $message, + 'data' => $data + ]); +}
\ No newline at end of file diff --git a/public/upload.php b/public/upload.php index 2ff9e96..2952315 100644 --- a/public/upload.php +++ b/public/upload.php @@ -1,21 +1,24 @@ <?php include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php'; +include_once $_SERVER['DOCUMENT_ROOT'] . '/../lib/utils.php'; if (!isset($_FILES['file'])) { - http_response_code(400); - exit("No 'file' specified!"); + json_response(null, "No 'file' specified!", 400); + exit(); } if (!is_dir(FILE_DIRECTORY) && !mkdir(FILE_DIRECTORY, 0777, true)) { - http_response_code(500); - exit("Failed to create a directory for user files"); + json_response(null, 'Failed to create a directory for user files', 500); + exit(); } $file = $_FILES['file']; if (!move_uploaded_file($file['tmp_name'], FILE_DIRECTORY . sprintf('/%s', $file['name']))) { - http_response_code(500); - exit("Failed to save the file. Try again later."); + json_response(null, 'Failed to save the file. Try again later.', 500); + exit(); } -header(sprintf('Location: /%s', $file['name']));
\ No newline at end of file +json_response([ + 'id' => $file['name'] +], null, 201);
\ No newline at end of file |
