-2

I'm programming a script that uploads mp3 to a folder in my server and adds the title, url and artist tot the 'audio' table.

but it dont work

PHP MySQL multiple mp3 upload in database

<?
$objConnect = mysql_connect("localhost","root","root") or die("Error Connect to Database");
$objDB = mysql_select_db("mydatabase");

for($i=0;$i<count($_FILES["filUpload"]["name"]);$i++)
$target_dir = "/mounted-storage/home150/sub007/sc80538-VHHY/website.com/audio/files/";
$trackName = $_FILES['foto']['name'];
$titel      = htmlspecialchars($_POST['titel']);
$artist     = $_POST['artiest'];
{
    if($_FILES["filUpload"]["name"][$i] != "")
    {
        if(move_uploaded_file($_FILES["filUpload"]["tmp_name"][$i],"myfile/".$_FILES["filUpload"]["name"][$i]))
        {
            //*** Insert Record ***//
            mysql_query("INSERT INTO `audio` (titel, url, categorie) values ('".$titel."', 'http://website.com/audio/files/".$trackName."','".$artist."')");
        }
    }
}

echo "Copy/Upload Complete<br>"; ?>

and the HTML form :

 <form name="form1" method="post" action="upload.php" enctype="multipart/form-data">
Track1 : <input name="titel" type="text" id="Titel" size="63" /><p>
 <input type="file" name="filUpload[]"><br>
    <br><br>
    Track2 : <input name="titel" type="text" id="Titel" size="63" /><p>
 <input type="file" name="filUpload[]"><br>
    <br><br>
    Track3 : <input name="titel" type="text" id="Titel" size="63" /><p>
 <input type="file" name="filUpload[]"><br>
    <br><br>
    Track4 : <input name="titel" type="text" id="Titel" size="63" /><p>
 <input type="file" name="filUpload[]"><br>
    <br><br>
    Track5 : <input name="titel" type="text" id="Titel" size="63" /><p>
 <input type="file" name="filUpload[]"><br>
    <br><br>
    Track6 : <input name="titel" type="text" id="Titel" size="63" /><p>
 <input type="file" name="filUpload[]"><br>
    <br><br>
    <select name="artiest">
              <?php
$query = mysql_query("SELECT * FROM artiesten ORDER BY naam ASC");
while ($array = mysql_fetch_assoc($query)){
 echo "<option value=\"". $array['naam'] ."\">". $array['naam']. "</option>";
}
?>
            </select>
    
 <input name="btnSubmit" type="submit" value="Submit">
 </form>
Fouad
  • 17
  • 1
  • 3

1 Answers1

0

this is just a sample code you have to fit in your website and add mysql connection ware i leave comment or just include external file

php code

if (isset($_POST["upload"])){


    $userdir = "mp3/songs/";
    for($i=0; $i<count($_FILES['mp3']['name']); $i++) {

    $target_file = $userdir . basename($_FILES["mp3"]["name"][$i]);
    $imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);




            if (!file_exists($userdir)){
             mkdir($userdir, 0777, true);
            }
        if (move_uploaded_file($_FILES["mp3"]["tmp_name"][$i], $target_file)) {

            // your mysql connect code or you have to be included external file with $connect variable or just rename it 
            $song = $_FILES["mp3"]["name"][0];
             $sql = sprintf(
                            "INSERT INTO music (mp3) VALUES ('%s')",
                            mysqli_escape_string($connect, $song)
                            );

                            $res = mysqli_query($connect,$sql);

            echo "The file  has been uploaded.\n";
        } else {

            echo "Sorry, there was an error uploading your file.\n";
        }

  }
}

html code :

<form action="<?php echo $_SERVER["PHP_SELF"]; ?>" method="post" enctype="multipart/form-data">
<input type="file" name="mp3[]" multiple="true" >
<input type="submit" name="upload" value="upload">
</form>
yahoo5000
  • 470
  • 6
  • 18
  • hey thank you my friend for your answer but i need to add also the titlename of each track in the database – Fouad Dec 20 '15 at 17:56
  • the first i think you should name each input field like title1 title2 and ect just to identifie witch is witch and later just input them the same way i show u with an mp3 example `$sql = sprintf( "INSERT INTO music (title,mp3) VALUES ('%s', '%s')", mysqli_escape_string($connect, $title),mysqli_escape_string($connect, $song) );` title probably will be eaqual to $_POST["title1"]; do this will all of your fields or use `for or while to do this ` – yahoo5000 Dec 20 '15 at 18:24