So I ran into this today.
Was attempting to validate an uploaded CSV file's MIME type by looking at $_FILES['upload_file']['type']
, but for certain users on various browsers (and not necessarily the same browsers between said users; for instance it worked fine for me in FF but for another user it didn't work on FF) the $_FILES['upload_file']['type']
was coming up as "application/vnd.ms-excel" instead of the expected "text/csv" or "text/plain".
So I resorted to using the (IMHO) much more reliable finfo_* functions something like this:
$acceptable_mime_types = array('text/plain', 'text/csv', 'text/comma-separated-values');
if (!empty($_FILES) && array_key_exists('upload_file', $_FILES) && $_FILES['upload_file']['error'] == UPLOAD_ERR_OK) {
$tmpf = $_FILES['upload_file']['tmp_name'];
// Make sure $tmpf is kosher, then:
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime_type = finfo_file($finfo, $tmpf);
if (!in_array($mime_type, $acceptable_mime_types)) {
// Unacceptable mime type.
}
}