60 lines
1.1 KiB
PHP
60 lines
1.1 KiB
PHP
<?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"
|
|
]);
|
|
}
|