Sujan Shrestha
Sujan Shrestha

Reputation: 614

How to upload multiple images with the same order of selection in php

I want to upload multiple images with the same order of selection. for example if i select 5 images and the selection order as a.jpg , b.jpg, c.jpg, d.jpg, e.jpg but my code is uploading these images in reverse order. 1st selected image is uploaded last and last selected image is uploaded first. i could not understand how image ordering works in such case. if i select b.jpg 1st, e.jpg 2nd and a.jpg 3rd then it should upload these images with the same order as b.jpg, e.jpg, a.jpg. my code is as follows.

      include('lib/myclass.php');
      include('simpleimage.php');

      for($i=0;$i<count($_FILES['file']['name']);$i++)
      { 

        $image_name = $_FILES['file']['name'][$i];
        $split = explode(".", $image_name);
        $extension = $split[count($split)-1];
        $name = $split[count($split)-2];
        $filename = $db_product.$name.rand().'.'.$extension;


        if(move_uploaded_file($_FILES['file']['tmp_name'][$i],'product/'.$filename))
        {
          $image = new SimpleImage();
          $image->load('product/'.$filename);
          $image->resize(60,50);
          $image->save('product/small/'.$filename); 

          $db_insert="insert into tbl_products_image(Id, Rno, Image)values('null','".$_POST['rrno']."','".$filename."')";
          $obj->insert($db_insert);
         }
      }

and my form is

 <form name="file_upload" id="file_upload" method="post" action="upload_file.php" target="frame1" enctype="multipart/form-data" >
   <input type="file" id="file" name="file[]" multiple onchange="this.form.submit(); display_block();" /> 
 </form>

how can i upload multiple images with the same order of image selection ?

Upvotes: 3

Views: 1530

Answers (1)

Nana Partykar
Nana Partykar

Reputation: 10548

Since, you have dicovered that images are going in reverse order. Then change the loop. It might help.

include('lib/myclass.php');
include('simpleimage.php');

$TotalImage=count($_FILES['file']['name']);

for($i=$TotalImage;$i>0;$i--) //Change Loop in reverse order. 
{ 

    $image_name = $_FILES['file']['name'][$i];
    $split = explode(".", $image_name);
    $extension = $split[count($split)-1];
    $name = $split[count($split)-2];
    $filename = $db_product.$name.rand().'.'.$extension;


    if(move_uploaded_file($_FILES['file']['tmp_name'][$i],'product/'.$filename))
    {
      $image = new SimpleImage();
      $image->load('product/'.$filename);
      $image->resize(60,50);
      $image->save('product/small/'.$filename); 

      $db_insert="insert into tbl_products_image(Id, Rno, Image)values('null','".$_POST['rrno']."','".$filename."')";
      $obj->insert($db_insert);
     }
}

Upvotes: 1

Related Questions