Jump to content

mp3 filtering by preg_match fails


rubing

Recommended Posts

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

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);
}

It still does not work, but at least now we can debut it!  :D  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 ) )

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']));
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.