How can PHP developers effectively validate and restrict file formats for uploaded images, such as only allowing gif, jpeg, or jpg formats?

To validate and restrict file formats for uploaded images in PHP, developers can use the `$_FILES` superglobal to access the file type of the uploaded image and then check if it matches the allowed formats (e.g., gif, jpeg, jpg). If the file type does not match the allowed formats, an error message can be displayed to the user.

$allowedFormats = ['image/gif', 'image/jpeg', 'image/jpg'];

if (isset($_FILES['image']) && $_FILES['image']['error'] === UPLOAD_ERR_OK) {
    $fileType = $_FILES['image']['type'];
    
    if (!in_array($fileType, $allowedFormats)) {
        echo "Only gif, jpeg, and jpg formats are allowed for image uploads.";
    } else {
        // Process the uploaded image
    }
}