How can PHP be used to compare the file size of an uploaded file with a predefined maximum size limit?

To compare the file size of an uploaded file with a predefined maximum size limit in PHP, you can use the $_FILES superglobal array to access the uploaded file's size and compare it with the maximum size limit. If the uploaded file exceeds the maximum size limit, you can display an error message to the user.

$maxFileSize = 5 * 1024 * 1024; // 5MB
if ($_FILES['file']['size'] > $maxFileSize) {
    echo "Error: File size exceeds the maximum limit of 5MB.";
} else {
    // Proceed with file upload process
}