What role does Base64 encoding play in file uploads in PHP, and how can it affect file size limits?

Base64 encoding is commonly used in file uploads in PHP to convert binary data into a text-based format that can be safely transmitted over the web. However, Base64 encoding increases the size of the file by approximately 33%, which can affect file size limits set by servers or frameworks. To mitigate this issue, it's important to be aware of the increased file size when using Base64 encoding and adjust the file size limits accordingly.

// Set the maximum file size limit taking into account the increased size due to Base64 encoding
$maxFileSize = 2 * 1024 * 1024; // 2MB limit for Base64 encoded files

// Check if the uploaded file size exceeds the limit
if ($_FILES['file']['size'] > $maxFileSize) {
    // Handle error for exceeding file size limit
    echo "File size exceeds the limit.";
}