How can file uploads be securely validated in PHP, considering both file type and content?

File uploads in PHP should be securely validated by checking both the file type and content to prevent malicious uploads. This can be done by using functions like `mime_content_type()` to verify the file type and by checking the file content to ensure it matches the expected format. Additionally, setting restrictions on file size and renaming the file upon upload can also enhance security.

// Validate file type and content
$allowedTypes = ['image/jpeg', 'image/png'];
$maxFileSize = 1048576; // 1MB

if (in_array(mime_content_type($_FILES['file']['tmp_name']), $allowedTypes) && $_FILES['file']['size'] <= $maxFileSize) {
    // File type and size are valid, process the upload
    move_uploaded_file($_FILES['file']['tmp_name'], 'uploads/' . $_FILES['file']['name']);
    echo 'File uploaded successfully.';
} else {
    // Invalid file type or size
    echo 'Invalid file type or size.';
}