如何上传mp3文件

Gle*_*enR 0 php

我正在尝试获取将 mp3 或某些图像文件上传到文件夹的表单。
使用下面的代码我可以上传图像文件,但是当我尝试上传 mp3 时,我收到错误Undefined index: file in C:\wamp\www\recordlabel\inc\soundclips.php on line 6并且代码invalid file从我的 else 块中回显。任何人都可以提供帮助吗?

 $allowedExts = array("mp3", "jpeg", "jpg", "png");
 $temp = explode(".", $_FILES["file"]["name"]);
 $extension = end($temp);

 if ((($_FILES["file"]["type"] == "audio/mp3")
        || ($_FILES["file"]["type"] == "image/jpeg")
        || ($_FILES["file"]["type"] == "image/jpg")
        || ($_FILES["file"]["type"] == "image/pjpeg")
        || ($_FILES["file"]["type"] == "image/x-png")
        || ($_FILES["file"]["type"] == "image/png"))
        && in_array($extension, $allowedExts))
{
   if ($_FILES["file"]["error"] > 0)
   {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
   }
   else
   {
      echo "Upload: " . $_FILES["file"]["name"] . "<br>";
      echo "Type: " . $_FILES["file"]["type"] . "<br>";
      echo "Size: " . ($_FILES["file"]["size"] / 1024) . " kB<br>";
      echo "Temp file: " . $_FILES["file"]["tmp_name"] . "<br>";

      if (file_exists("../soundclips/" . $_FILES["file"]["name"]))
      {
         echo $_FILES["file"]["name"] . " already exists. ";
      }
      else
      {
         move_uploaded_file($_FILES["file"]["tmp_name"],
         "../soundclips/" . $_FILES["file"]["name"]);
         echo "Stored in: " . "../soundclips/" . $_FILES["file"]["name"];
      }
   }
}
else
{
    echo "Invalid file";
}
Run Code Online (Sandbox Code Playgroud)

这是我的表格

<form class='form1' action='../inc/soundclips.php' method='post'     
                                                   enctype='multipart/form-data'>
   <input type="hidden" name="id" value="<?php echo $pid; ?>"/>
   <b>Add Soundclip For <i><?php echo $e;?></i> </b> 
   <?php echo"<divclass='editimage'>";
   echo "<img class='resizedimage' src='{$row['image']}' />";
   echo"</div>";?><br /> 
   <b>Song</b><br /><input type=text size='60' name='asong' /><br />
   <input name='file' type="file" id="file"  /><br />
   <input type='submit' name='add' value='Add Soundclip' />
</form>
Run Code Online (Sandbox Code Playgroud)

小智 6

看起来您的 mp3 文件无法上传,因此它在$_FILES数组中丢失。这可能是由于它与图像文件相比的大小。

请检查upload_max_filesizepost_max_size设置您的php.ini并允许比您的 mp3 文件更大的大小。