rubing Posted November 29, 2008 Share Posted November 29, 2008 I am trying to allow mp3 uploading with the following code: $blacklist = array('.mp3'); foreach($blacklist as $item) { if(!preg_match('/$item\$/i',$_FILES['uploadedfile']['name'])) { echo "Sorry unsupported file type...mp3's only\n"; echo "<br />"; print_r($_FILES); exit; } } Now, when I attempt to upload an mp3 file this is the response I am getting: Sorry unsupported file type...mp3's only Array ( [uploadedfile] => Array ( [name] => 07-madlib_the_beat_konducta-all_virtue.mp3 [type] => audio/mpeg [tmp_name] => /tmp/phpA74Crx [error] => 0 => 2408445 ) ) Link to comment https://forums.phpfreaks.com/topic/134763-mp3-filtering-by-preg_match-fails/ Share on other sites More sharing options...
DarkWater Posted November 29, 2008 Share Posted November 29, 2008 Why would you use preg_match for this? $allowed = array('.mp3'); $ext = strrchr('.', $_FILES['uploadedfile']['name']); if (!in_array($ext, $allowed)) { echo "Bad file ext...whatever else you want to put here.<br />"; print_r($_FILES); exit(1); } Link to comment https://forums.phpfreaks.com/topic/134763-mp3-filtering-by-preg_match-fails/#findComment-701729 Share on other sites More sharing options...
rubing Posted November 29, 2008 Author Share Posted November 29, 2008 It still does not work, but at least now we can debut it! Looks like the extension is not being picked out ??? Here is my new code: $_FILES['uploadedfile']['name']=trim($_FILES['uploadedfile']['name']); $_FILES['uploadedfile']['name']=ereg_replace("[^A-Za-z0-9.]", "", $_FILES['uploadedfile']['name']); $allowed = array('.mp3','.MP3'); $ext = strrchr('.', $_FILES['uploadedfile']['name']); if (!in_array($ext, $allowed)) { echo "Bad file ext...<br />"; echo "ext:"; echo $ext; echo "<br />"; print_r($_FILES); exit(1); } And here is the response: Bad file ext... ext: Array ( [uploadedfile] => Array ( [name] => 05madlibthebeatkonductagambleonyaboyfeat.defari.mp3 [type] => audio/mpeg [tmp_name] => /tmp/php1CtZ2n [error] => 0 => 5242805 ) ) Link to comment https://forums.phpfreaks.com/topic/134763-mp3-filtering-by-preg_match-fails/#findComment-701794 Share on other sites More sharing options...
DarkWater Posted November 29, 2008 Share Posted November 29, 2008 My bad, I switched the strrchr() arguments. $ext = strrchr($_FILES['uploadedfile']['name'], '.'); Link to comment https://forums.phpfreaks.com/topic/134763-mp3-filtering-by-preg_match-fails/#findComment-701798 Share on other sites More sharing options...
flyhoney Posted November 29, 2008 Share Posted November 29, 2008 Remember you can use $_FILES['userfile']['type'] to get the mime type, which for mp3 should be 'audio/mpeg'. <?php $_FILES['uploadedfile']['name'] = trim($_FILES['uploadedfile']['name']); $_FILES['uploadedfile']['name'] = ereg_replace("[^A-Za-z0-9.]", '', $_FILES['uploadedfile']['name']); $allowed = array('audio/mpeg'); if (!in_array($_FILES['uploadedfile']['type'], $allowed)) { echo "Bad file...<br />"; echo "type:"; echo $_FILES['uploadedfile']['type']; echo "<br />"; print_r($_FILES); exit(1); } ?> Also notice, instead of having to do: <?php $allowed = array('.mp3', '.MP3'); ?> do a strtolower on the ext: <?php $ext = strtolower(strrchr('.', $_FILES['uploadedfile']['name'])); ?> Link to comment https://forums.phpfreaks.com/topic/134763-mp3-filtering-by-preg_match-fails/#findComment-701800 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.