2

I'm new here.

I need to upload Multiple mp3 files to a directory and store them in the 'audio' table in mysql.

I'm using this script but it works only with one file, it's annoying to do one track each time. That's why I need a multiple upload script.

I wish to only write the title of each track like this:

php: file1.mp3 [0] file2.mp3 [1] file3.mp3 [2]

html form: File 1 title: .... File 2 title: .... File 3 title: ....

insert to 'audio' 

Sorry for my bad English. i hope you know what I mean

<?php if(isset($_POST['kkupload'])){ 
    $filename = $_FILES['foto']['name'];
    $extensie = substr($filename, -3);

    $map = "/mounted-storage/home150/sub007/sc80538-VHHY//audio/files/";
    $file = $_FILES['foto'];

    $breedte = $_FILES['foto'];
    $max_bytes = 100000000000;


    if(strtolower($extensie) != "mp3" && strtolower($extensie) != "jpg" && strtolower($extensie) != "jpeg" && strtolower($extensie) != "png" && strtolower($extensie) != "bmp")
    {
        echo "Je kan alleen .gif, .jpg .jpeg en .png bestanden uploaden!";
    }

    elseif($_FILES['foto']['size'] > $max_bytes) { echo("Het bestand is groter dan ".$max_bytes." bytes!"); }

    else {

    $length = strlen($filename);
    $name = "pict";
    $name = substr($filename, 0, $length - 4);
    $i = "1";
    $tempname = $name;

    $picName = $_FILES['foto']['name'];
    $titel      = htmlspecialchars($_POST['titel']);
    $bericht    = $_POST['bericht'];
    $url        = htmlspecialchars($_POST['pica']);
    $youtube    = $_POST['youtube'];
    $nr = rand(0,99999999999);
    if(file_exists($_FILES['foto']['name']))
    {
        $picName = $nr. $_FILES['foto']['name'];
        if(file_exists($picName))
        {
            $picName = $nr. $_FILES['foto']['name'];
        }       
    }

    move_uploaded_file($_FILES['foto']['tmp_name'], $map.$_FILES['foto']['name']."") or die("Fout met uploaden plaatje");
    mysql_query("INSERT INTO `audio` (titel, url, categorie) values ('".$titel."', '/audio/files/".$picName."','".$bericht."')");
    echo "je hebt succesvol nieuws geupload!";      }}?>

<form action="?pagina=addnieuws" method="post" enctype="multipart/form-data" name="form1" id="form1">
            <table width="100%" border="0" cellpadding="2" cellspacing="2" id="form1">
              <tr>
                <td width="77"><b> <font size="2" face="Verdana">Tite track:</font></b>
                    </div></td>
                <td><font size="2">
                  <input name="titel" type="text" id="Titel" size="63" />
                </font></td>
              </tr>
              <tr>
                <td width="77"><b> <font size="2" face="Verdana">Plaatje:</font></b>
                    </div></td>
                <td><font size="2">
                  <input type="file" name="foto" size="52" />
                  </font><b><font size="1" face="Verdana"> <br />
                    MP3</font></b></td>
              </tr>
              <tr>
                <td valign="top" width="77"><b> <font size="2" face="Verdana">Artiest:</font></b>
                  </div></td>
                <td>
  <script type="text/javascript">
bkLib.onDomLoaded(function() {
new nicEditor({fullPanel : true}).panelInstance('area2'); 
});
</script>
  <select name="bericht">
              <?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>
  </td>
              </tr>
              <tr>
                <td width="77" colspan="2"><font size="2">
                  <input type="submit" name="kkupload" value="Upload" />
                </font></td>
              </tr>
            </table>
          </form>
Rohit Gupta
  • 4,022
  • 20
  • 31
  • 41
Fouad
  • 17
  • 1
  • 3
  • 1
    Possible duplicate of [Multiple file upload in php](http://stackoverflow.com/questions/2704314/multiple-file-upload-in-php) – Rajdeep Paul Dec 19 '15 at 18:26
  • Am I right in thinking you want your code inside a function which is called on post? As that is what I think your trying to say but I may be wrong – Juakali92 Dec 19 '15 at 18:29
  • I need to add more title field for the [2] , [3] files and a loop for the uploads to send them directly to the database. i hope you understand my – Fouad Dec 19 '15 at 18:35
  • I smell SQL injection. Also, don't mix your logic code with the markup. Create classes to represent objects. – TheRealChx101 Dec 19 '15 at 19:01

1 Answers1

0

The code below is a example to give you a idea of how to upload more then one file at a time but to a folder

for($i=0; $i < count($_FILES['filesToUpload']['name']); $i++){
$target_dir = "../uploads/";
$target_file = $target_dir . basename($_FILES['filesToUpload']['name'][$i]);
$uploadOk = 1;

Insert your security check code here between the above and below code to see if the file really is a mp3 and etc.

if ($uploadOk == 0) {
    echo "Sorry, your file was not uploaded. ";
} else {
if (move_uploaded_file($_FILES["filesToUpload"]["tmp_name"][$i], $target_file)) {
        echo "The file ". basename( $_FILES["filesToUpload"]["name"][$i]). " has been uploaded. ";
    } else {
        echo "Sorry, there was an error uploading your file. ";
    }
}
}

Also, depending on the type of database you're using, probably best to save the files in a folder and not directly inside the database so that you're not over-bloating the database.. You instead would need to save its file path to the database and then open from the file path. Though that's just giving you a suggestion.

Lastly, that's just the PHP side of it, not sure what you'll need for the SQL part. Though I guess that's fine since you mainly asked for how to do multiple file uploads.

Rami
  • 67
  • 1
  • 12