add types, add comments and add error handling
This commit is contained in:
parent
3027dbdd5b
commit
47256117d1
70
upload.php
70
upload.php
@ -1,8 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
// Start time of the script
|
|
||||||
$before = microtime(true); // 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
|
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
|
$tokens = array("set me"); // Your secret keys
|
||||||
$uploadDir = "./"; // The upload directory
|
$uploadDir = "./"; // The upload directory
|
||||||
$useRandomFileNames = false; // Use random file names instead of the original file name
|
$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
|
* Check if the token is valid
|
||||||
*/
|
*/
|
||||||
function checkToken($token) {
|
function checkToken($token): bool {
|
||||||
global $tokens;
|
global $tokens;
|
||||||
if (in_array($token, $tokens)) {
|
return isset($token) && in_array($token, $tokens);
|
||||||
return true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Generate a random string
|
* Generate a random string
|
||||||
*/
|
*/
|
||||||
function generateRandomString($length = 10) {
|
function generateRandomString($length = 10): string {
|
||||||
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
|
||||||
$charactersLength = strlen($characters);
|
$charactersLength = strlen($characters);
|
||||||
$randomString = '';
|
$randomString = '';
|
||||||
@ -37,55 +37,56 @@ function generateRandomString($length = 10) {
|
|||||||
* Return a JSON response
|
* Return a JSON response
|
||||||
*/
|
*/
|
||||||
function returnJson($status, $message, $timeTaken = null) {
|
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));
|
echo(json_encode($json));
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$token = $_POST['secret']; // The provided secret key
|
try {
|
||||||
$file = $_FILES['sharex']; // The uploaded file
|
$token = $_POST['secret']; // The provided secret key
|
||||||
|
$file = $_FILES['sharex']; // The uploaded file
|
||||||
|
|
||||||
// Check if the token is valid
|
// Check if the token is valid
|
||||||
if (!checkToken($token)) {
|
if (!checkToken($token)) {
|
||||||
$timeTaken = microtime(true) - $before;
|
$timeTaken = microtime(true) - $before;
|
||||||
returnJson('ERROR', 'Invalid or missing secret key', $timeTaken);
|
returnJson('ERROR', 'Invalid or missing secret key', $timeTaken);
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check if the file was uploaded
|
// Check if the file was uploaded
|
||||||
if (!isset($file)) {
|
if (!isset($file)) {
|
||||||
$timeTaken = microtime(true) - $before;
|
$timeTaken = microtime(true) - $before;
|
||||||
returnJson('ERROR', 'No file was uploaded', $timeTaken);
|
returnJson('ERROR', 'No file was uploaded', $timeTaken);
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$target_file = $_FILES["sharex"]["name"]; // File name
|
$target_file = $_FILES["sharex"]["name"]; // File name
|
||||||
$fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.)
|
$fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.)
|
||||||
|
|
||||||
// Check if the file already exists
|
// Check if the file already exists
|
||||||
if (file_exists($uploadDir . $target_file)) {
|
if (file_exists($uploadDir . $target_file)) {
|
||||||
$timeTaken = microtime(true) - $before;
|
$timeTaken = microtime(true) - $before;
|
||||||
returnJson('ERROR', 'File already exists', $timeTaken);
|
returnJson('ERROR', 'File already exists', $timeTaken);
|
||||||
die();
|
die();
|
||||||
}
|
}
|
||||||
|
|
||||||
$shouldSave = true; // Whether or not the file should be saved
|
$shouldSave = true; // Whether or not the file should be saved
|
||||||
$finalName = $target_file; // The final name of the file
|
$finalName = $target_file; // The final name of the file
|
||||||
if ($useRandomFileNames) { // Generate a random file name if enabled
|
if ($useRandomFileNames) { // Generate a random file name if enabled
|
||||||
$finalName = generateRandomString($fileNameLength) . "." . $fileType;
|
$finalName = generateRandomString($fileNameLength) . "." . $fileType;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Convert the image to webp if applicable
|
// Convert the image to webp if applicable
|
||||||
if (in_array($fileType, array("png", "jpeg", "jpg"))) {
|
if (in_array($fileType, array("png", "jpeg", "jpg"))) {
|
||||||
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
|
$image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"]));
|
||||||
$webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp";
|
$webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp";
|
||||||
imagewebp($image, $webp_file, 90);
|
imagewebp($image, $webp_file, 90);
|
||||||
imagedestroy($image);
|
imagedestroy($image);
|
||||||
$finalName = $webp_file;
|
$finalName = $webp_file;
|
||||||
$shouldSave = false;
|
$shouldSave = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($shouldSave) {
|
if ($shouldSave) {
|
||||||
// Move the file to the uploads folder
|
// Move the file to the uploads folder
|
||||||
if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) {
|
if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) {
|
||||||
$timeTaken = microtime(true) - $before;
|
$timeTaken = microtime(true) - $before;
|
||||||
@ -95,6 +96,11 @@ if ($shouldSave) {
|
|||||||
returnJson('ERROR', 'File upload failed. Does the folder exist and did you CHMOD the folder?', $timeTaken);
|
returnJson('ERROR', 'File upload failed. Does the folder exist and did you CHMOD the folder?', $timeTaken);
|
||||||
}
|
}
|
||||||
die();
|
die();
|
||||||
|
}
|
||||||
|
returnJson('OK', $finalName, $timeTaken);
|
||||||
|
} catch (Exception $e) {
|
||||||
|
$timeTaken = microtime(true) - $before;
|
||||||
|
returnJson('ERROR', $e->getMessage(), $timeTaken);
|
||||||
|
die();
|
||||||
}
|
}
|
||||||
returnJson('OK', $finalName, $timeTaken);
|
|
||||||
?>
|
?>
|
Loading…
Reference in New Issue
Block a user