How can the MAX_FILE_SIZE attribute in HTML forms be utilized to restrict file uploads in PHP, and what limitations does it have?

To restrict file uploads in PHP using the MAX_FILE_SIZE attribute in HTML forms, you can set a maximum file size limit that the server will accept for file uploads. This attribute should be placed within the form tag and specifies the maximum file size in bytes that can be uploaded. However, it is important to note that this attribute is not foolproof as it can be easily manipulated by users, so additional server-side validation should also be implemented.

// HTML form with MAX_FILE_SIZE attribute set to limit file size
<form action="upload.php" method="post" enctype="multipart/form-data">
    <input type="hidden" name="MAX_FILE_SIZE" value="1000000"> <!-- 1MB limit -->
    <input type="file" name="fileToUpload">
    <input type="submit" value="Upload File">
</form>

// PHP code in upload.php to check file size
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $maxFileSize = 1000000; // 1MB limit
    if ($_FILES['fileToUpload']['size'] > $maxFileSize) {
        echo "File size exceeds the limit.";
    } else {
        // Process file upload
    }
}
?>