0

Hi,
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.

<?php 
 //file upload


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

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

    $overwrite=0;
    $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";
                break;
           }

     }

    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];

        $file_type=$_FILES['files']['type'][$key];

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

                $fsize=$max_file_size/1001048576;
                return    'File size must be less than '.$fsize.' MB';
                break;

            }
        }

        if(is_dir($file_dir)==false){

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

               if($status < 1){

                     return "unable to create  diractory $file_dir ";

                }

        }

        if(is_dir($file_dir)){

            if($overwrite < 1){

                move_uploaded_file($file_tmp_name,"$file_dir/".$file_name);

            }

        }
        $catid=$_POST['catid'];
        $postid=$_POST['postid'];

        $a=$row_rcdSort['MAX(recordListingID)'];
        $b=1;

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

   }

        return "Success";

}

?>

      </h3>
    <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; ?>" />

<?php
echo $row_rcdSort['MAX(recordListingID)']+1 ;
?>
    </form>

Any ideas? Thanks in advance.

2
Contributors
1
Reply
9
Views
3 Years
Discussion Span
Last Post by diafol
0

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) 
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.