adimoh
adimoh

Reputation: 728

PHP upload script not working in Firefox but in Chrome

I am trying to use this PHP script to check extension of file while upload. The problem is that it works perfectly in Chrome but in Firefox it is always returning false even when the extension is correct and so "Invalid File" is being echoed always.

    $allowedExts = array("mp3", "wma", "wav", "ogg", "aac", "aiff", "amr", "ra");
    $arr = explode(".", $_FILES["file"]["name"]);
    $extension = end($arr);

    if ((($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/wma")
    || ($_FILES["file"]["type"] == "audio/wav")
    || ($_FILES["file"]["type"] == "audio/ogg")
    || ($_FILES["file"]["type"] == "audio/aac")
    || ($_FILES["file"]["type"] == "audio/aiff")
    || ($_FILES["file"]["type"] == "audio/amr")
    || ($_FILES["file"]["type"] == "audio/ra"))
    && ($_FILES["file"]["size"] < 20000000)
    && in_array($extension, $allowedExts))
      {
       .
       .
       .
       .
      }
    else
      {
      echo "Invalid file";
      }

Can anyone please tell what is the problem in the if section that it's not working in Firefox?

Upvotes: 1

Views: 1843

Answers (2)

Johndave Decano
Johndave Decano

Reputation: 2113

It is because the file type has audio/type name and your matching it to just the type.

Try using in_array function and remove the audio/ using preg_replace

$allowedExts = array("mp3", "wma", "wav", "ogg", "aac", "aiff", "amr", "ra");
$allowedSize = 20000000;
$currentType = preg_replace('/(.*)(/)/','',$_FILES['file']['type']);
$currentSize = $_FILES['file']['size'];

if( in_array($currentType,$allowedExts) && $currentSize < $allowedSize ) 
{
    // DO UPLOAD 
    // REFER TO http://php.net/manual/en/function.move-uploaded-file.php
}
else
{
    // ERROR UPLOADING FILE
}

Upvotes: 1

transilvlad
transilvlad

Reputation: 14532

PHP works the same regardless of the browser.

You need to look at your HTML code.

That is where the problem lies.

Upvotes: 0

Related Questions