'ERROR', 'message' => 'Invalid or missing upload secret', // Remove this if you don't want to show the support URL 'support' => "For support, visit - https://git.fascinated.cc/Fascinated/sharex-php-uploader", 'timeTaken' => getTimeTaken() )); die(); } // Check if the file was uploaded if (!isset($file)) { returnJson(array( 'status' => 'ERROR', 'message' => 'No file was uploaded', 'timeTaken' => getTimeTaken() )); die(); } $target_file = preg_replace("/[^A-Za-z0-9_.]/", '', $_FILES["sharex"]["name"]); // Remove unwanted characters $fileType = pathinfo($target_file, PATHINFO_EXTENSION); // File extension (e.g. png, jpg, etc.) // Check if the file already exists if (file_exists($uploadDir . $target_file)) { returnJson(array( 'status' => 'ERROR', 'message' => 'File already exists', 'timeTaken' => getTimeTaken() )); die(); } $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; } // Convert the image to webp if applicable if (in_array($fileType, array("png", "jpeg", "jpg")) && $_FILES["sharex"]["size"] > $webpThreadhold && $shouldConvertToWebp) { $image = imagecreatefromstring(file_get_contents($_FILES["sharex"]["tmp_name"])); $webp_file = pathinfo($finalName, PATHINFO_FILENAME) . ".webp"; imagewebp($image, $webp_file, $webpQuality); // Convert the image and save it imagedestroy($image); // Free up memory $finalName = $webp_file; $shouldSave = false; } if ($shouldSave) { // Move the file to the uploads folder if (move_uploaded_file($_FILES["sharex"]["tmp_name"], $uploadDir . $finalName)) { returnJson(array( 'status' => 'OK', 'message' => 'File uploaded successfully', 'timeTaken' => getTimeTaken() )); } else { returnJson(array( 'status' => 'ERROR', 'message' => 'Failed to save file. Check the permissions of the upload directory.', 'timeTaken' => getTimeTaken() )); } die(); } returnJson(array( 'status' => 'OK', 'message' => 'File uploaded successfully', 'timeTaken' => getTimeTaken() )); die(); } catch (Exception $e) { // Handle any errors returnJson(array( 'status' => 'ERROR', 'message' => $e->getMessage(), 'timeTaken' => getTimeTaken() )); die(); }