doulan2827
2013-07-26 10:17
浏览 57
已采纳

php文件上传 - MIME类型无效?

I have a script that uploads a file. It checks the mime-type (should be audio file) and then uploads it. The type seems to be recognized but it gives an "invalid file" output. Am I missing something obvious?

Upload: songname.mp3
Type: audio/mpeg
Size: 4.5997314453125 MB
Invalid file 

And part of the script:

if ($_FILES["file"]["error"] > 0)
  {
  echo "Error: " . $_FILES["file"]["error"] . "<br>";
  }
else
  {
  echo "Upload: " . $_FILES["file"]["name"] . "<br>";
  echo "Type: " . $_FILES["file"]["type"] . "<br>";
  echo "Size: " . ($_FILES["file"]["size"] / (1024*1024)) . " MB<br>";
  }
  $allowedExts = array("gif", "jpeg", "jpg", "png");
$extension = end(explode(".", $_FILES["file"]["name"]));
if ((($_FILES["file"]["type"] == "audio/mpeg")
|| ($_FILES["file"]["type"] == "audio/x-mpeg")
|| ($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/x-mp3")
|| ($_FILES["file"]["type"] == "audio/mpeg3")
|| ($_FILES["file"]["type"] == "audio/x-mpeg3")
|| ($_FILES["file"]["type"] == "audio/mpg")
|| ($_FILES["file"]["type"] == "audio/x-mpg")
|| ($_FILES["file"]["type"] == "audio/x-mpegaudio")
|| ($_FILES["file"]["type"] == "audio/wav")
|| ($_FILES["file"]["type"] == "audio/x-wav")
|| ($_FILES["file"]["type"] == "audio/ogg"))   
&& ($_FILES["file"]["size"] < 20000000)
&& in_array($extension, $allowedExts))
  {;;
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
  else
    {//something
    }

    if (file_exists("../sites/default/files/dance/" . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "../sites/default/files/dance/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "../sites/default/files/dance/" . $_FILES["file"]["name"];
      }    
  }
else
  {
  echo "Invalid file";
  }
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • du42561 2013-07-26 10:19
    已采纳

    you have allowed extensions like this:

     $allowedExts = array("gif", "jpeg", "jpg", "png");
    

    you should try at least:

     $allowedExts = array("mp3","wav","ogg");
    
    已采纳该答案
    打赏 评论

相关推荐 更多相似问题