add types, add comments and add error handling
This commit is contained in:
parent
3027dbdd5b
commit
47256117d1
26
upload.php
26
upload.php
@ -1,8 +1,12 @@
|
||||
<?php
|
||||
// Start time of the script
|
||||
$before = microtime(true); // Start time of the script
|
||||
header('Content-type:application/json;charset=utf-8'); // Set the content type to JSON
|
||||
declare(strict_types=1);
|
||||
error_reporting(E_ERROR); // Hide PHP errors
|
||||
header('Content-type:application/json;charset=utf-8'); // Set the content type to JSON
|
||||
|
||||
/**
|
||||
* Configuration
|
||||
*/
|
||||
$tokens = array("set me"); // Your secret keys
|
||||
$uploadDir = "./"; // The upload directory
|
||||
$useRandomFileNames = false; // Use random file names instead of the original file name
|
||||
@ -11,19 +15,15 @@ $fileNameLength = 8; // The length of the random file name
|
||||
/**
|
||||
* Check if the token is valid
|
||||
*/
|
||||
function checkToken($token) {
|
||||
function checkToken($token): bool {
|
||||
global $tokens;
|
||||
if (in_array($token, $tokens)) {
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
return isset($token) && in_array($token, $tokens);
|
||||
}
|
||||
|
||||
/**
|
||||
* Generate a random string
|
||||
*/
|
||||
function generateRandomString($length = 10) {
|
||||
function generateRandomString($length = 10): string {
|
||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||
$charactersLength = strlen($characters);
|
||||
$randomString = '';
|
||||
@ -37,11 +37,12 @@ function generateRandomString($length = 10) {
|
||||
* Return a JSON response
|
||||
*/
|
||||
function returnJson($status, $message, $timeTaken = null) {
|
||||
$json = array('status' => $status, 'url' => $message, 'processingTime' => round($timeTaken, 2) . "ms");
|
||||
$json = array('status' => $status, 'url' => $message, 'processingTime' => round($timeTaken ?? 0, 2) . "ms");
|
||||
echo(json_encode($json));
|
||||
die();
|
||||
}
|
||||
|
||||
try {
|
||||
$token = $_POST['secret']; // The provided secret key
|
||||
$file = $_FILES['sharex']; // The uploaded file
|
||||
|
||||
@ -97,4 +98,9 @@ if ($shouldSave) {
|
||||
die();
|
||||
}
|
||||
returnJson('OK', $finalName, $timeTaken);
|
||||
} catch (Exception $e) {
|
||||
$timeTaken = microtime(true) - $before;
|
||||
returnJson('ERROR', $e->getMessage(), $timeTaken);
|
||||
die();
|
||||
}
|
||||
?>
|
Loading…
Reference in New Issue
Block a user