Jump to content

[SOLVED] Restricting Upload File Type to MP3 Only


sh0wtym3

Recommended Posts

This code WAS working before, I edited the form that uses it a little, and now for some reason it won't work correctly. When I try to upload MP3s now it says:

 

"1.mp3 cannot be uploaded. The only acceptable file format is .mp3"

 

Which, of course, makes no sense. I tried to include every possible mp3 file type inside my array. Thanks in advance.

 

// allow MIME file types
$filetype = array('audio/mpeg','audio/mpeg3','audio/x-mpeg','audio/x-mpeg3','audio/x-mpeg-3','audio/mp3','audio/x-mp3','audio/x-mpeg-aac','audio/mpg','audio/mpg3','audio/mp4');
$ftype = false;

// check if uploaded file type is allowed
foreach($filetype as $type) {

if ($type == $_FILES['frmfile']['type']) {

$ftype = true;
break;

}

}

if ($ftype && $fsize && $_POST['frmname'] != '') {

switch($_FILES['frmfile']['error']) {

case 0:

//rename the file
$username = $_COOKIE['ID_my_site'];
$new_file_name=UPL_FLD.$username.'_'.$file;

//determine the filesize variable
$filesize = $_FILES['frmfile']['size'];

//Makes sure genre field was filled in
if (!$_POST['genre']) {
$msg = 'Please select a genre.';
break;
}

//Makes sure desc field was filled in
if (!$_POST['desc']) {
$msg = 'Please add a description.';
break;
}

// Insert a row of information into the table "music"
putenv("TZ=US/Eastern");
$title = $_POST['frmname'];
$desc = $_POST['desc'];
$genre = $_POST['genre'];
$timestamp = date('m/d/y g:ia');
mysql_query("INSERT INTO music 
(title, desc, genre, timestamp) VALUES('$title','$desc','$genre','$timestamp')") 
or die(mysql_error());  

// move file to the upload folder
$upload = move_uploaded_file($_FILES['frmfile']['tmp_name'],$new_file_name);
if ($upload) {
$msg = $_FILES['frmfile']['name'].' uploaded successfully';
} else {

$msg = 'Error. Please try again.';
}
break;

case 3:
$msg = 'Error. Please try again.';
break;

default:
$msg = 'Error - please contact administrator';

}

} elseif ($_FILES['frmfile']['error'] == 4) {

$msg = 'Please select file';

} elseif ($_POST['frmname'] == '') {

$msg = 'Please add a title to your song';

} else {
$msg = $_FILES['frmfile']['name'].' cannot be uploaded.';
if(!$ftype) {
$msg .= ' The only acceptable file format is .mp3';
}
if(!$fsize) {
$msg .= 'Maximum file size is '.$maxfs;
}

} 

}

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.