Search code examples
phpimagegetimagesize

How to check if the uploaded file is a supported file


i'm uploading a tif file, but i always get the echo 'The uploaded file type is not supported'. Why? How do i check if file is supported according to my array of supported files

//image types supported
$types = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_TIFF);

list($width, $height, $type) = getimagesize($_FILES[$name]['tmp_name']);
$ext = image_type_to_extension($type); // get the extension

if (!isset($types[$type])) {
   echo = 'The uploaded file type is not supported';
} else
   echo = 'All good!';
}

Solution

  • You're checking if $types has a key $type rather than a value $type. Instead use in_array.
    Also form http://www.php.net/manual/en/function.image-type-to-mime-type.php, there is no IMAGETYPE_TIFF but instead IMAGETYPE_TIFF_II and IMAGETYPE_TIFF_MM

    $types = array(IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM);
    ...
    if (in_arry($type,$types)) {