How does the getimagesize() function in PHP help in determining the type of an uploaded image file?

The getimagesize() function in PHP can be used to determine the type of an uploaded image file by returning an array containing the image's dimensions and type. By checking the returned image type value, we can determine if the uploaded file is an image file and handle it accordingly.

// Check if the uploaded file is an image
$image_info = getimagesize($_FILES["fileToUpload"]["tmp_name"]);
if($image_info !== false) {
    $image_type = $image_info[2];
    if($image_type == IMAGETYPE_JPEG || $image_type == IMAGETYPE_PNG || $image_type == IMAGETYPE_GIF) {
        // Handle the uploaded image file
    } else {
        // Not a valid image file
    }
} else {
    // Error getting image size
}