Are there any potential pitfalls in solely relying on file extensions to verify image files in PHP?

Relying solely on file extensions to verify image files in PHP can be risky as file extensions can be easily manipulated. To ensure the file is truly an image, it's recommended to use additional methods such as checking the MIME type of the file.

// Check both file extension and MIME type to verify image file
function isImageFile($file) {
    $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif'];
    $allowedMimeTypes = ['image/jpeg', 'image/png', 'image/gif'];

    $fileExtension = pathinfo($file['name'], PATHINFO_EXTENSION);
    $fileMimeType = mime_content_type($file['tmp_name']);

    if (in_array($fileExtension, $allowedExtensions) && in_array($fileMimeType, $allowedMimeTypes)) {
        return true;
    } else {
        return false;
    }
}

// Example usage
if (isImageFile($_FILES['image'])) {
    // Process the image file
} else {
    echo "Invalid image file";
}