Good evening, I'm having trouble trying to upload an audio file when the form is submitted. the input type is named 'user_music' within the form.
I'm trying to run a check & create a unique folder name every time the audio file successfully gets submitted. So that eventually every audio file that has just been submitted gets saved and moved into the unique folder-name that has just been created. Then I then would like it to store the file destination of that audiofile into the "user_music" field of the datbase table "music" so that I can use it later on. That's all I'm trying to do.
I'm trying to implement something like this into what I currently have, however I have no idea how to go about doing it:
if (isset($_FILES['user_music'])) {
if ((($_FILES["user_music"]["type"]=="audio/mp3") || (@$_FILES["user_music"]["type"]=="audio/ogg") || ($_FILES["user_music"]["type"]=="audio/wav"))&&($_FILES["user_music"]["size"] < 15048576)) //15 Megabyte
{
$safe = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$mus_rand = substr(str_shuffle($safe), 0, 15);
mkdir("userdata/mms/$mus_rand");
}
Im trying to implement the above into what I currently have, which is this:
$uploads_dir = '/userdata/mms';
foreach ($_FILES["user_music"]["error"] as $key => $error) {
if ($error == UPLOAD_ERR_OK) {
$tmp_name = $_FILES["user_music"]["tmp_name"][$key];
$name = $_FILES["user_music"]["name"][$key];
move_uploaded_file($tmp_name, "$uploads_dir/$name");
$music_query = "INSERT INTO music WHERE user_music='$mus_rand/$music_name' WHERE username='$user'"
}
}
This is my connect script:
$db = mysql_connect("localhost","root","Mylife2015") or die ("Couldn't
connect to SQL server");
mysql_select_db("i-neo") or die("Couldn't select DB");
session_start();
if (isset($_SESSION['user_login'])) {
$user = $_SESSION["user_login"];
}
else {
$user = "";
}
As you can see below the submit button for the entire form is named 'upl'.
This is the insert query with prepared statements (I'm still trying to get the hang of prepared statements):
$upl = @$_POST['upl'];
if ($upl){
$query = "INSERT INTO music ( , $user, $man, $sn, $an, $sa, $um, $d)
VALUES (?,?,?,?,?,?,?,?)";
$stmt = $db->prepare($query);
$stmt->bind_param("sssssbbs", $user, $man, $sn, $an, $sa, $um, $d); //
bind variables
$un = $user; // User Logged in
$man = "artist_name"; // Artist Name
$sn = "audio_name"; // Audio Name
$an = "album_name"; // Album Name
$sa = "song_art"; // Song's Art Cover
$um = "user_music"; // Audio File
$d = "d-m-y h:i:s a"; // Date
$stmt->execute()
}
Below is just my php form and database table if needed
This is my php form:
<form action="upload.php" method="POST">
<input type="text" name="artist_name" placeholder="Artist's Name"/><br/ <br/>
<input type="text" name="audio_name" placeholder="Song Name"/><br/><br/>
<input type="text" name="album_name" placeholder="Album Name"/><br/><br/>
<input type="file" name="song_art" /><br/> Song Art Picture<br/>
<input type="file" name="user_music" />Music File<br/>
<input type="submit" name="upl" value="Upload"/>
</form>
Lastly this is the table that is being inserted to:
CREATE TABLE IF NOT EXISTS `music` (
`id` int(11) NOT NULL,
`username` varchar(255) NOT NULL,
`artist_name` varchar(255) NOT NULL,
`audio_name` varchar(255) NOT NULL,
`album_name` varchar(255) NOT NULL,
`song_art` text NOT NULL,
`user_music` text NOT NULL,
`date_uplaoded` datetime NOT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=latin1;