Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php how to use getimagesize() to check image type on upload [duplicate]

Possible Duplicate:
GetImageSize() not returning FALSE when it should

i currently have a filter system as follows:

   // Check to see if the type of file uploaded is a valid image type
function is_valid_type($file)
{
    // This is an array that holds all the valid image MIME types
    $valid_types = array("image/jpg", "image/JPG", "image/jpeg", "image/bmp", "image/gif", "image/png");

    if (in_array($file['type'], $valid_types))
        return 1;
    return 0;
}

but i have been told that it is better to check the filetype myself, how would i use the getimagesize() to check the filetype in a similar way?

like image 229
neeko Avatar asked Dec 06 '22 11:12

neeko


2 Answers

getimagesize() returns an array with 7 elements. The index 2 of the array contains one of the IMAGETYPE_XXX constants indicating the type of the image.

The equivalent of the function provided using getimagesize() would be

function is_valid_type($file)
{
    $size = getimagesize($file);
    if(!$size) {
        return 0;
    }

    $valid_types = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP);

    if(in_array($size[2],  $valid_types)) {
        return 1;
    } else {
        return 0;
    }
}
like image 70
user1704650 Avatar answered Dec 11 '22 09:12

user1704650


You can use as below

$img_info   = getimagesize($_FILES['image']['tmp_name']);
$mime   = $img_info['mime']; // mime-type as string for ex. "image/jpeg" etc.
like image 25
GBD Avatar answered Dec 11 '22 08:12

GBD