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

When accepting file uploads in PHP, it is important to validate both the file type and size to prevent malicious files from being uploaded to the server. To validate file types, you can use the `mime_content_type()` function to check the MIME type of the file. To validate file sizes, you can compare the size of the uploaded file with a predefined maximum size limit.

// Validate file type
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
$uploadedFileType = mime_content_type($_FILES['file']['tmp_name']);

if (!in_array($uploadedFileType, $allowedTypes)) {
    // File type is not allowed
    die('Invalid file type');
}

// Validate file size
$maxFileSize = 1048576; // 1 MB
$uploadedFileSize = $_FILES['file']['size'];

if ($uploadedFileSize > $maxFileSize) {
    // File size exceeds limit
    die('File size is too large');
}

// File type and size are valid, proceed with file upload