spbrad
spbrad

Reputation: 33

PHP - checking file type using exif_imagetype error

I am trying to check that an uploaded image is a PNG, JPG or GIF and not just check the file extension. I am trying the following:

$allowed_types = array (IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
$detectedType = exif_imagetype($_FILES['file_to_upload']['tmp_name']);
    if ( !in_array($detectedType, $allowed_types) ) {
        die ( 'Please upload a pdf or an image ' );
    }


//code to handle image

However I am receiving an alert even if it is an image. Can anyone point me towards why?

Upvotes: 0

Views: 2024

Answers (1)

spbrad
spbrad

Reputation: 33

should have been:

 $allowed_types = array (IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF);
 $detectedType = exif_imagetype($_FILES['file']['tmp_name']);
    if ( !in_array($detectedType, $allowed_types) ) {
    die ( 'Please upload a pdf or an image ' );
  }

Upvotes: 1

Related Questions