zhangy Posted August 31, 2009 Share Posted August 31, 2009 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 Quote Link to comment Share on other sites More sharing options...
wildteen88 Posted August 31, 2009 Share Posted August 31, 2009 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'; } Quote Link to comment Share on other sites More sharing options...
zhangy Posted August 31, 2009 Author Share Posted August 31, 2009 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.) Quote Link to comment Share on other sites More sharing options...
zhangy Posted September 1, 2009 Author Share Posted September 1, 2009 *blump* Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.