diff options
| author | ilotterytea <iltsu@alright.party> | 2025-03-18 02:30:19 +0500 |
|---|---|---|
| committer | ilotterytea <iltsu@alright.party> | 2025-03-18 02:30:19 +0500 |
| commit | ad1f0a370d432701bc5fe4eb6fa1fb23f94c1293 (patch) | |
| tree | d60ebd22fd92c0d49eb0d3693f4bca79e127bf78 /public | |
| parent | e34bce13c5916d424743f2220cd454824a4292b6 (diff) | |
feat: file upload
Diffstat (limited to 'public')
| -rw-r--r-- | public/index.php | 2 | ||||
| -rw-r--r-- | public/upload.php | 21 |
2 files changed, 22 insertions, 1 deletions
diff --git a/public/index.php b/public/index.php index 3f3dd98..b9880fe 100644 --- a/public/index.php +++ b/public/index.php @@ -33,7 +33,7 @@ $accepted_mime_types = implode(', ', $accepted_mime_types); <p>File Upload</p> </div> <div class="content"> - <form action="/upload.php" method="post" enctype="multipart/form-data" column="column gap-8"> + <form action="/upload.php" method="post" enctype="multipart/form-data" class="column gap-8"> <input type="file" name="file" required accept="<?= $accepted_mime_types ?>"> <button type="submit">Upload</button> </form> diff --git a/public/upload.php b/public/upload.php new file mode 100644 index 0000000..2ff9e96 --- /dev/null +++ b/public/upload.php @@ -0,0 +1,21 @@ +<?php +include_once $_SERVER['DOCUMENT_ROOT'] . '/../config.php'; + +if (!isset($_FILES['file'])) { + http_response_code(400); + exit("No 'file' specified!"); +} + +if (!is_dir(FILE_DIRECTORY) && !mkdir(FILE_DIRECTORY, 0777, true)) { + http_response_code(500); + exit("Failed to create a directory for user files"); +} + +$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."); +} + +header(sprintf('Location: /%s', $file['name']));
\ No newline at end of file |
