multiple files upload

See all posts See thread Reply

Re: multiple files upload new!
by Sergio, 16 years, 9 months ago
I already saw that question and include that in code. Here is the code that I'm trying to making useful:

include_once '../classes/class.upload.php';
$time = time();
for($p = 0; $p <= 2; $p++) {
  $file = 'file'.$p;
  $k = $p+1;
  if (!empty($HTTP_POST_FILES['my_field']['name'][$p])) {
    if (!C_IMG_ERR) {
      // If Unavailable image upload errors with UIN
      $dir = date("mY", $time);
      $slash = "/";
    } else $dir = $slash = "";
    $fb = date("dHis", $time);
    $fe = rand(0, 999);
    $fn = $fb."-".$fe;

    $handle = new upload($_FILES['my_field']);
    $this_upload = array();

    $dir = date("mY", $time);$slash="/";
    $fb=date("dHis",$time);$fe=rand(0,999);$fn =$fb."-".$fe;
    $intpic = $fn.'.';
    $handle->jpeg_quality = 70;
    $handle->image_watermark = 'watermark.png';
    $handle->image_watermark_position = 'B';
    $handle->file_new_name_body = $intpic;
    $handle->Process("uploads/$dir");

    if ($handle->uploaded) {
      if ($handle->processed) {
        $this_upload['large'] = $handle->file_dst_name;
      }
    } 

    $handle->image_resize            = true;
    $handle->image_ratio_y           = true;
    $handle->image_x                 = 80;
    $handle->file_new_name_body = $intpic;
    $handle->Process("uploads/tmb/$dir");
  
    if ($handle->processed) {
      // store the small image filename  
      $this_upload['small'] = $handle->file_dst_name;
      $handle->clean();
    } 
  
    // add this set of pictures to the main array
    $uploaded[] = $this_upload;
  }  
  if ($uploaded[0]['large'] !='') $d1=$dir.'/';
  if ($uploaded[1]['large'] !='') $d2=$dir.'/';
  if ($uploaded[2]['large'] !='') $d3=$dir.'/';

  $mpic[1]=", pic1='".$d1.$uploaded[0]['large']."'";
  $mpic[2]=", pic2='".$d2.$uploaded[1]['large']."'";
  $mpic[3]=", pic3='".$d3.$uploaded[2]['large']."'";
}


Input file field has name "my_field[]"Reply
Re: multiple files upload new!
by colin, 16 years, 9 months ago
Read the FAQ again. If you do multiple uploads, you have to change the $_FILES array.

$files = array();
foreach ($_FILES['my_field'] as $k => $l) {
 foreach ($l as $i => $v) {
 if (!array_key_exists($i, $files)) 
   $files[$i] = array();
   $files[$i][$k] = $v;
 }
} 

Then you can use $files rather than $_FILES in your code. Note that you can trim out all non-uploaded images in the code above.Reply