Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is there a mime type inconsistency with firefox, chrome?

Tags:

php

I was puzzled at first why my files weren't uploading for some users and I found out it was everyone who wasn't using chrome which was the browser I was testing.

Basically I'm doing a file check to make sure they are only able to upload mp3s.

This this was working for chrome, but not firefox.

if ($_FILES['uploaded']['type']=="audio/mp3")

This was working for firefox, but not chrome.

$_FILES['uploaded']['type']=="audio/mpeg"

Could anyone explain why this is happening? I would think both browsers would be able to understand either or... Are there any other browsers I might need to worry about touchy mime types like these?

Edit: If what Pekka suggested is true, what would be the best way to check for a certain mime type?

like image 496
Tek Avatar asked Feb 06 '10 22:02

Tek


People also ask

How does browser determine MIME type?

During the ObjectType step in the request handling process, the server determines the MIME type attributes of the resource requested by the client. Several different server application functions (SAFs) can be used to determine the MIME type, but the most commonly used one is type-by-extension.


1 Answers

According to w3schools, audio/mpeg is the correct type. But it doesn't matter, MIME types can vary, you absolutely can't rely on them when checking files. Inconsistencies are the rule, and to be expected.

To identify a MP3 file, maybe the getid3 package can help you:

getID3() is a PHP script that extracts useful information from MP3s & other multimedia file formats.

Edit: IANA has an official list of MIME types here. There is no mention of mp3 there, so this is buggy behaviour on Chrome's part.

Edit 2: Your best bet on server side to determine the MIME type of a file is the finfo extension. It tries to determine the type of a file by "content sniffing", looking for specific characteristics of certain file types in the first few bytes of the data. In this process, MIME types can also vary, but at least they are consistent on the same server, so you won't have browser issues any more.

like image 171
Pekka Avatar answered Oct 06 '22 01:10

Pekka