How can one validate and sanitize user input related to image file types in PHP to prevent potential security risks?

To validate and sanitize user input related to image file types in PHP, one can use the `$_FILES` superglobal to check the file type before processing it. One should also use functions like `pathinfo()` to extract the file extension and verify that it is an allowed image type. Additionally, using functions like `move_uploaded_file()` to handle file uploads securely can help prevent security risks.

// Validate and sanitize user input related to image file types
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
$uploadDir = 'uploads/';

if(isset($_FILES['image']) && $_FILES['image']['error'] == UPLOAD_ERR_OK) {
    $fileInfo = pathinfo($_FILES['image']['name']);
    $fileExtension = strtolower($fileInfo['extension']);

    if(in_array($fileExtension, $allowedExtensions)) {
        $uploadPath = $uploadDir . basename($_FILES['image']['name']);
        move_uploaded_file($_FILES['image']['tmp_name'], $uploadPath);
        echo "File uploaded successfully.";
    } else {
        echo "Invalid file type. Only JPG, JPEG, PNG, and GIF files are allowed.";
    }
} else {
    echo "Error uploading file.";
}