How can I only allow certain filetypes on upload in php?

Put the allowed types in an array and use in_array().

$file_type = $_FILES['foreign_character_upload']['type']; //returns the mimetype

$allowed = array("image/jpeg", "image/gif", "application/pdf");
if(!in_array($file_type, $allowed)) {
  $error_message="Only jpg, gif, and pdf files are allowed.";
  $error="yes";
}

Leave a Comment