Jump to content

Uploading Audio Files


zhangy

Recommended Posts

Hi everyone,

I am trying something new so please forgive my lack of understanding here, but what I am trying to do is upload audio files to a mysql database.

From what I can gather from some online tutorials is that the php code for uploading audio files is no different than uploading anyother file. Is that so?

 

As of now I am trying to use the following which was taken from an online tutorial:

<?php
if ((($_FILES["file"]["type"] == "audio/mp3")
|| ($_FILES["file"]["type"] == "audio/mp4")
|| ($_FILES["file"]["type"] == "audio/wav"))
&& ($_FILES["file"]["size"] < 1000000))
  {
  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";
  }
?> 

 

 

This code, to me, doesnt seem to use a database because of its lacking an insert query. My question is how to best change this code around so that it works with a mysql database?

 

Thanks  ;)

Link to comment
Share on other sites

You wouldnt need to change the code much.

 

You'll first need to connect to mysql at the start of the script. Then to insert the file in to the database you'll place your insert query where these lines are

      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];

 

I'd store just the file name within the database not the file itself. So as an example I'd replace the three lines above with this

      $mp3_name =  $_FILES["file"]["name"];
      $mp3_path =  "upload/$mp3_name";

      move_uploaded_file($_FILES["file"]["tmp_name"], $mp3_path);

      $sql = "INSERT INTO mp3_files_table SET name='$mp3_name', path='$mp3_path'";
      $result = mysql_query($sql);
      if($result)
      {
            echo 'MP3 Upload successfully';
      }
      else
      {
            echo 'ERROR: Upload Failed';
      }

Link to comment
Share on other sites

Thanks wildteen88 for the fast and helpful response!

Why not store the file itself though? I mean how else would there be access to the song?

(What I was imagining were artists uploading the song that they want played on the website.)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.