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
https://forums.phpfreaks.com/topic/172572-uploading-audio-files/
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';
      }

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.