How can error messages be properly displayed when uploading images with different file types in PHP?

When uploading images with different file types in PHP, it is important to properly handle error messages to inform users if their file is not in the correct format. This can be achieved by checking the file type before uploading and displaying an error message if it does not match the allowed types.

<?php
$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];

if (in_array($_FILES['image']['type'], $allowedTypes)) {
    // Upload the image
} else {
    echo "Error: Only JPEG, PNG, and GIF files are allowed.";
}
?>