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");
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进
  • ¥15 关于#java#的问题,请各位专家解答!