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 the开发者_JS百科re 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?
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.
Each browser may implement the W3c standards slightly differently, much to the chagrin of all developers (#rant)...
Relying on meta information generated by a user/browser is highly unreliable and not recommended. If this is your only security/sanity mechanism then someone wishing to cause harm to your system could manually enter the meta type field with a custom request to == "audio/mpeg" then upload any kind of executable file. If you really want to be sure you must fully examine the data on the server side once it has been uploaded before accepting it into your permanent storage / production system. Or on a less sinister note.. a user with a different browser you have not tested before could want to upload a legit mp3 file but it may not announce its mime type as audio/mpeg or audio/mp3 and then you would deny them access to the system...
audio tag
<audio controls="controls" autoplay="true">
<source id="webm" src="Mash Up 2011 19 songs in 1 DJ Got Us Falling In Love Mandu DeeJay Remix.ogg" type="audio/ogg" />
<source id="webm" src="Mash Up 2011 19 songs in 1 DJ Got Us Falling In Love Mandu DeeJay Remix.mp3" type="audio/mp3" />
<source id="webm" src="Mash Up 2011 19 songs in 1 DJ Got Us Falling In Love Mandu DeeJay Remix.webm" type="audio/webm" />
<!--[if lt IE 9]>
<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="150" height="38" id="niftyPlayer1" align="right">
<param name=movie value="niftyplayer.swf?file=Mash Up 2011 19 songs in 1 DJ Got Us Falling In Love Mandu DeeJay Remix.mp3&as=1">
<param name=quality value=high>
<param name=bgcolor value=#471313>
<embed src="niftyplayer.swf?file=Mash Up 2011 19 songs in 1 DJ Got Us Falling In Love Mandu DeeJay Remix.mp3&as=1" quality=high bgcolor=#471313 width="150" height="38" name="niftyPlayer1" align="right" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">
</embed>
</object>
<!--<![endif]-->
</audio>
Video Tag
<video width="400" height="300" controls="controls" poster="img/poster.png" >
<source id="3gp" src="wedding1.3gp" type="video/3gp" />
<source id="mp4" src="perfectparties/weddingDroid.mp4" type="video/mp4" />
<source id="webm" src="wedding1.webm" type="video/webm" />
<source id="mp4" src="perfectparties/wedding1.mp4" type="video/mp4" />
<source id="ogv" src="perfectparties/wedding1.ogv" type="video/ogv" />
<!--[if lt IE 9]>
<object id="FlashID" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="400" height="300">
<param name="autoplay" value="false">
<param name="movie" value="wedding1.swf">
<param name="quality" value="high">
<param name="wmode" value="opaque">
<param name="swfversion" value="6.0.65.0">
<param name="expressinstall" value="Scripts/expressInstall.swf">
</object>
<!--<![endif]-->
</video>
<script type="text/javascript">
swfobject.registerObject("FlashID");
</script>
It requires working through all formats...
MOST IMPORTANTLY MAKE SURE YOUR SYSTEM ADMINISTRATOR HAS ALLOWED THE MIME TYPES TO BE USED ON THE SERVER/ROOT/DOMAIN. This is the biggest inconsistency but this works across all browsers including IE.
精彩评论