From 3027dbdd5b17ee6eb10f919dceee7bf091d19d5b Mon Sep 17 00:00:00 2001 From: Fascinated Date: Sun, 9 Apr 2023 12:17:17 +0100 Subject: [PATCH] Fixed webp files and added random file length configuration --- upload.php | 54 ++++++++++++++++++++++++++++++++++++++++-------------- 1 file changed, 40 insertions(+), 14 deletions(-) diff --git a/upload.php b/upload.php index 0743b90..0e60b92 100644 --- a/upload.php +++ b/upload.php @@ -5,6 +5,8 @@ header('Content-type:application/json;charset=utf-8'); // Set the content type t error_reporting(E_ERROR); // Hide PHP errors $tokens = array("set me"); // Your secret keys $uploadDir = "./"; // The upload directory +$useRandomFileNames = false; // Use random file names instead of the original file name +$fileNameLength = 8; // The length of the random file name /** * Check if the token is valid @@ -18,6 +20,19 @@ function checkToken($token) { } } +/** + * Generate a random string + */ +function generateRandomString($length = 10) { + $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; + $charactersLength = strlen($characters); + $randomString = ''; + for ($i = 0; $i < $length; $i++) { + $randomString .= $characters[rand(0, $charactersLength - 1)]; + } + return $randomString; +} + /** * Return a JSON response */ @@ -54,21 +69,32 @@ if (file_exists($uploadDir . $target_file)) { die(); } -if (in_array($fileType, array("png", "jpeg", "jpg"))) { - // Convert to webp - $image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"])); - $webp_file = pathinfo($target_file, PATHINFO_FILENAME) . ".webp"; - imagewebp($image, $webp_file, 90); - imagedestroy($image); - $target_file = $webp_file; +$shouldSave = true; // Whether or not the file should be saved +$finalName = $target_file; // The final name of the file +if ($useRandomFileNames) { // Generate a random file name if enabled + $finalName = generateRandomString($fileNameLength) . "." . $fileType; } -// Move the file to the uploads folder -if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $target_file)) { - $timeTaken = microtime(true) - $before; - returnJson('OK', $target_file, $timeTaken); -} else { - $timeTaken = microtime(true) - $before; - returnJson('ERROR', 'File upload failed. Does the folder exist and did you CHMOD the folder?', $timeTaken); +// Convert the image to webp if applicable +if (in_array($fileType, array("png", "jpeg", "jpg"))) { + $image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"])); + $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; + imagewebp($image, $webp_file, 90); + imagedestroy($image); + $finalName = $webp_file; + $shouldSave = false; } + +if ($shouldSave) { + // Move the file to the uploads folder + if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) { + $timeTaken = microtime(true) - $before; + returnJson('OK', $finalName, $timeTaken); + } else { + $timeTaken = microtime(true) - $before; + returnJson('ERROR', 'File upload failed. Does the folder exist and did you CHMOD the folder?', $timeTaken); + } + die(); +} +returnJson('OK', $finalName, $timeTaken); ?> \ No newline at end of file