This commit is contained in:
Lee 2023-04-15 14:13:25 +01:00
commit cd66ea36f3
2 changed files with 106 additions and 0 deletions

47
index.html Normal file

@ -0,0 +1,47 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>WIP Uploader</title>
<script src="https://cdn.fascinated.cc/assets/tailwindcss/3.2.4.js"></script>
</head>
<body>
<!-- Centered div, vertial and horizontal -->
<!-- uploads file to upload.php, shows map id after upload -->
<!-- Modern design -->
<div class="flex items-center justify-center h-screen">
<div class="w-1/2">
<div class="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4">
<p class="text-gray-700 text-2xl font-bold mb-4">WIP Map Uploader</p>
<form action="upload.php" method="post" enctype="multipart/form-data">
<div class="mb-4">
<label
class="block text-gray-700 text-sm font-bold mb-2"
for="file"
>
Select a file to upload
</label>
<input
class="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline"
id="file"
name="file"
type="file"
/>
</div>
<div class="flex items-center justify-between">
<button
class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline"
type="submit"
>
Upload
</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>

59
upload.php Normal file

@ -0,0 +1,59 @@
<?php
header('Content-type:application/json;charset=utf-8'); // Set the response content type to JSON
$target_dir = "./maps/";
/**
* Generates a random map id
*/
function generateMapId()
{
$mapId = "";
$characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
for ($i = 0; $i < 10; $i++) {
$mapId .= $characters[rand(0, strlen($characters) - 1)];
}
return $mapId;
}
/**
* Return a JSON response
*/
function returnJson($data): void
{
echo (json_encode($data));
die();
}
/**
* Checks if the file is a zip file
*/
function isZipFile($file)
{
$fileType = strtolower(pathinfo($file, PATHINFO_EXTENSION));
return $fileType == "zip";
}
$mapId = generateMapId(); // the id of the map
$map = $_FILES["map"];
$file = $map["tmp_name"];
if (!isZipFile($file)) {
echo "The file is not a zip file";
return;
}
$target_file = $target_dir . $mapId . ".zip";
if (move_uploaded_file($file, $target_file)) {
returnJson([
"status" => "success",
"mapId" => $mapId
]);
} else {
returnJson([
"status" => "error",
"message" => "An error occurred while uploading the file"
]);
}