How can the PHP script be modified to display an error message for incorrect file formats?

To display an error message for incorrect file formats in a PHP script, you can check the file format before processing it and display an error message if it does not match the expected format. This can be done by using a conditional statement to compare the file format with the allowed formats and then displaying an error message if it does not match.

<?php
$allowedFormats = ['jpg', 'jpeg', 'png', 'gif'];
$uploadedFile = $_FILES['file']['name'];
$uploadedFileFormat = pathinfo($uploadedFile, PATHINFO_EXTENSION);

if (!in_array($uploadedFileFormat, $allowedFormats)) {
    echo "Error: Incorrect file format. Please upload a file in JPG, JPEG, PNG, or GIF format.";
} else {
    // Process the file if it has the correct format
}
?>