user8753781
user8753781

Reputation:

uploading audio file from mozilla firefox in php not working

when I upload an audio file with chrome it uploads fluently without any error but when I am uploading it in firefox it doesn't give me an error but doesn't upload my file either this is my code

$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
          $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
          if ((
            ($_FILES["file"]["type"] == "audio/mp3")
            || ($_FILES["file"]["type"] == "audio/wma")
            )
            && in_array($extension, $allowedExts))
          {
            $temp = explode(".", $_FILES["file"]["name"]);
            $audio = date('YmdHis') . '.' . end($temp);
            move_uploaded_file($_FILES["file"]["tmp_name"],
            "uploads/top_songs/" . $audio);
          }
          else{
            $audio = "";
          }

Upvotes: 0

Views: 80

Answers (1)

RareStrawbery
RareStrawbery

Reputation: 36

Mozilla Firefox gives audio/mpeg MIME type to mp3 files according to the standart RFC 3003, unlike chrome which gives audio/mp3.

The correct solution would be

$allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
$allowedMimes = array("audio/mp3", "audio/wma", "audio/mpeg");
$extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
if(in_array($_FILES["file"]["type"], $allowedMimes) && in_array($extension, $allowedExts)){
    $temp = explode(".", $_FILES["file"]["name"]);
    $audio = date('YmdHis') . '.' . end($temp);

    move_uploaded_file($_FILES["file"]["tmp_name"], "uploads/top_songs/" . $audio);
}else{
    $audio = "";
}

Upvotes: 1

Related Questions