php how to use getimagesize() to check image type on upload
Solution 1
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;
}
}
Solution 2
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.
Solution 3
Firstly check if getimagesize
returns false
. If it does, then the file is not a recognised image format (or not an image at all).
Otherwise, get index 2 of the returned array and run it through image_type_to_mime_type
. This will return a string like "image/gif" etc. See the docs for more info.
neeko
Updated on October 07, 2020Comments
-
neeko over 3 years
Possible Duplicate:
GetImageSize() not returning FALSE when it shouldi 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?
-
Baba over 11 yearsDid you test this code ???? you can not use
getimagesize on $_FILES['images']['tmp_name']
it would returnfalse
-
Baba over 11 yearsthat is interesting ... codepad.viper-7.com/N96kma it returns
Warning: getimagesize(): Filename cannot be empty
... it might be a PHP version specific issue -
neeko over 11 yearsthankyou for your reply, but this code returns an error in dreamweaver on the if(!$size){ line
-
user1704650 over 11 yearsThe third line was missing a semicolon, should be alright now.
-
neeko over 11 yearsnow i get this error Warning: getimagesize() expects parameter 1 to be string, array given
-
user1704650 over 11 yearsYou should be passing is_valid_type($file) function the filename as the parameter. You are probably giving it an $_FILES array. Try something like is_valid_type($file['name']). Or you could change the "getimagesize($file)" to "getimagesize($file['name'])" so you can call it the same way as the original function. But since the function needs only the filename, it doesn't make sense to have it expect an array.
-
neeko over 11 yearsthanks, but now i get this error 'getimagesize(538419_10151009993941091_424169609_n.jpg) [function.getimagesize]: failed to open stream: No such file or directory in...'
-
user1704650 over 11 yearsThe error is quite meaningful in this case: PHP can't find the file in question, most probably because it doesn't exist. You can try printing out the filename
$file
you're passing tois_valid_type($file)
and see if it exists for yourself. My guess is that the uploaded image is either saved to another folder or it is renamed during the upload. This is up to your implementation of things and not something related to theis_valid_type
function. That said, my take is that when you call the is_valid_type, the image hasn't yet been moved to it's final location: try$file['tmp_name']
. -
neeko over 11 yearsthankyou this is perfect, you have helped me so much!
-
Partack over 7 yearsSo, to summarize, usage of the above function is:
is_valid_type($_FILES['file']['tmp_name'])
if you're checking directly from the $_FILES array. I kinda wish i didn't have to read between the lines just to come to that conclusion. -
prospector almost 4 yearsthat code is susceptible to PHP Exif injection.