How can PHP developers ensure that only specific file types are allowed for upload in a form?

To ensure that only specific file types are allowed for upload in a form, PHP developers can check the file type before allowing the upload to proceed. This can be done by checking the MIME type of the uploaded file against a list of allowed MIME types. If the MIME type does not match any of the allowed types, the upload can be rejected.

$allowedTypes = ['image/jpeg', 'image/png', 'image/gif'];
$uploadedFileType = $_FILES['file']['type'];

if (!in_array($uploadedFileType, $allowedTypes)) {
    echo "Only JPEG, PNG, and GIF files are allowed for upload.";
    exit;
}

// Proceed with the upload process if the file type is allowed