I've got a page that allows the upload of multiple files at a time (while inserting filename to db). I've added the ability to arrange the file submission's display with a click and drag ui, this is also working fine...sort of. In order for the click and drag rearrange to work, each record has a recordListingID assigned at INSERT based upon the previous recordListingID inserted. This works fine for uploading single files. It isn't working on multiple files. If I upload five files, I get five identical recordListingIDs rather than them added in sequential order. Here is my code.

 //file upload

    $res =  upload_multiple_file($_FILES['files'],"uploaded_pictures");
    echo $res;

function upload_multiple_file($file,$file_dir="uploaded_pictures") {

    $allowed_file_type= array("pdf","ppt","pptx","xls"."xlxs","doc","docx","jpg", "jpeg", "png", "gif");
    $max_file_size = 1002097152;

     foreach($_FILES['files']['name'] as $fkey=> $fname){

         $ext = pathinfo($fname, PATHINFO_EXTENSION);
           if (!in_array($ext, $allowed_file_type)) {

               return "unsupported file format";


    foreach($_FILES['files']['tmp_name'] as $key => $tmp_name ){

        $file_name = $_FILES['files']['name'][$key];

        $file_size =$_FILES['files']['size'][$key];

        $file_tmp_name =$_FILES['files']['tmp_name'][$key];


        if($max_file_size_check >0) {
            if($file_size > $max_file_size){

                return    'File size must be less than '.$fsize.' MB';



              $status =  mkdir("$file_dir", 0700);

               if($status < 1){

                     return "unable to create  diractory $file_dir ";




            if($overwrite < 1){





        INSERT into picturesTest 
        (`postid`,`catid`,`picture`, `recordListingID`) 
        ('$postid','$catid', '$file_name', '$a+$b')";


        return "Success";



    <form action="" method="POST" enctype="multipart/form-data"  onsubmit="document.location='edit_photo.php?recordID=<?php echo $row_rcdPost['postid']; ?>'">
  <input type="file" name="files[]" multiple/>
<input type="submit" value="submit"/>
<input name="catid" type="hidden" id="catid" value="<?php echo $row_rcdPost['catid']; ?>" />
<input name="postid" type="hidden" id="postid" value="<?php echo $recordListingID; ?>" />

<input name="recordListingID" type="text" id="recordListingID" value=" <?php echo $row_rcdSort['MAX(recordListingID)']+1; ?>" />

echo $row_rcdSort['MAX(recordListingID)']+1 ;

Any ideas? Thanks in advance.

Member Avatar

Be careful of using multiple. IE9 and previous do not support it. So if you have users that may be using IE <= 9, they may get frustrated with having to use single file -> upload -> single file -> upload, etc.

I can't say I've studied your code closely, but why are you introducing the id into your INSERT SQL? Just leave off the id field from the statement and everything will be given an id automatically. ALso you can have multiple records in the VALUES clause, so you don't need to have a loop and run more than one SQL query. Just use a small loop to build up the statement...

INSERT INTO table field2, field5, field6 VALUES (val1,val2,val3),(valA,valB,valC) 
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.