How can PHP developers use functions like getimagesize() or mime_content_type() to validate the actual content type of uploaded files, rather than relying solely on the client-provided file type?

When uploading files, it's important to validate the actual content type of the file rather than relying solely on the client-provided file type, as this can be easily manipulated. PHP developers can use functions like getimagesize() or mime_content_type() to determine the actual content type of the uploaded file by inspecting its contents.

// Example code snippet to validate the actual content type of an uploaded file using getimagesize() function

$uploadedFile = $_FILES['file']['tmp_name'];

// Get the image size information
$imageSize = getimagesize($uploadedFile);

if ($imageSize !== false) {
    $mimeType = $imageSize['mime'];
    // Validate the mime type against a list of allowed types
    if (in_array($mimeType, ['image/jpeg', 'image/png', 'image/gif'])) {
        // File is an image of an allowed type
        echo 'Valid image file.';
    } else {
        // File is not an allowed image type
        echo 'Invalid image file type.';
    }
} else {
    // File is not a valid image
    echo 'Not a valid image file.';
}