How can the PHP function pathinfo be used to enhance the validation of file types in PHP?

When validating file types in PHP, it's important to not solely rely on the file extension as it can be easily manipulated. The PHP function pathinfo can be used to extract the file extension and then compare it against a list of allowed file types to enhance the validation process.

$allowedFileTypes = ['jpg', 'jpeg', 'png', 'gif'];
$filePath = 'path/to/file.jpg';

$fileInfo = pathinfo($filePath);
$fileExtension = strtolower($fileInfo['extension']);

if(in_array($fileExtension, $allowedFileTypes)) {
    echo 'File type is allowed.';
} else {
    echo 'Invalid file type.';
}