What are best practices for validating file types and sizes in PHP file uploads?

When allowing file uploads in PHP, it is important to validate the file types and sizes to prevent malicious files from being uploaded to the server. To validate file types, you can check the MIME type of the uploaded file using the `$_FILES['file']['type']` variable. To validate file sizes, you can check the `$_FILES['file']['size']` variable against a maximum allowed size.

// Validate file type
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
if (!in_array($_FILES['file']['type'], $allowedTypes)) {
    die('Invalid file type. Allowed types: jpeg, png, gif');
}

// Validate file size
$maxSize = 1048576; // 1MB
if ($_FILES['file']['size'] > $maxSize) {
    die('File size exceeds the limit of 1MB');
}