uploading an audio(mp3) file in php?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • impin
    New Member
    • Jul 2010
    • 127

    uploading an audio(mp3) file in php?

    i am uploading an audio file to a particular folder..

    its my form:

    Code:
    <form action="upload.php" method="post" enctype="multipart/form-data">
    <label for="file"><span>Filename:</span></label>
    <input type="file" name="file" id="file" /> 
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    its my upload.php
    Code:
    <?php
    
    $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
    $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    
    if ((($_FILES["file"]["type"] == "video/mp4")
    || ($_FILES["file"]["type"] == "audio/mp3")
    || ($_FILES["file"]["type"] == "audio/wma")
    || ($_FILES["file"]["type"] == "image/pjpeg")
    || ($_FILES["file"]["type"] == "image/gif")
    || ($_FILES["file"]["type"] == "image/jpeg"))
    
    && ($_FILES["file"]["size"] < 200000)
    && 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("upload/" . $_FILES["file"]["name"]))
          {
          echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
          move_uploaded_file($_FILES["file"]["tmp_name"],
          "upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    ?>
    the code works fine for images when i add audio file (mp3) i will get 'Invalid file" error.. the images uploaded to the upload folder without any problem.

    whats wrong in the code???
  • Mukesh9023
    New Member
    • Nov 2013
    • 11

    #2
    Code:
    <?php
    // make a folder upload to move your file.I yhink this code is necessary to modified but right now it working correctly.
    if(isset($_POST['submit']))
    {
    $allowedExts = array("jpg", "jpeg", "gif", "png", "mp3", "mp4", "wma");
    //echo $extension = pathinfo($_FILES['file']['name'], PATHINFO_EXTENSION);
    $fileName = $_FILES['file']['name'];
    $extension = substr($fileName, strrpos($fileName, '.') + 1); // getting the info about the image to get its extension
    
    /*if ((($_FILES["file"]["type"] == "video/mp4")|| ($_FILES["file"]["type"] == "audio/mp3")|| ($_FILES["file"]["type"] == "audio/wma")|| ($_FILES["file"]["type"] == "image/pjpeg")|| ($_FILES["file"]["type"] == "image/gif")|| ($_FILES["file"]["type"] == "image/jpeg")) && ($_FILES["file"]["size"] < 200000) && in_array($extension, $allowedExts))*/
    
    if(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("upload/" . $_FILES["file"]["name"]))
          {
            echo $_FILES["file"]["name"] . " already exists. ";
          }
        else
          {
           move_uploaded_file($_FILES["file"]["tmp_name"],"upload/" . $_FILES["file"]["name"]);
          echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
          }
        }
      }
    else
      {
      echo "Invalid file";
      }
    }
    ?>
    <form method="post"  enctype="multipart/form-data" >
    
    <label for="file"><span>Filename:</span></label>
    
    <input type="file" name="file" id="file" /> 
    
    <br />
    <input type="submit" name="submit" value="Submit" />
    </form>
    Last edited by Rabbit; Dec 13 '13, 04:11 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.

    Comment

    Working...