function UploadImage($POST,$FILES,$modul) {

  global $db,$ar;

  if(isset($POST[title]) && ($POST[title]!=)) {

      $title=$POST[title];

  } else {

      $title=;

  }

  if(isset($POST[caption]) && ($POST[caption]!=)) {

      $caption=$POST[caption];

  } else {

      $caption=;

  }

  //get last id

  $sql2= select id from `files` order by id desc limit 1;

  $res2=mysqli_query($db,$sql2);

  $r2=mysqli_fetch_assoc($res2);



  if(isset($r2[id]) && ($r2[id]>0)) {

      $iid=$r2[id]+1;

  } else {

      $iid=1;

  }



  //get last seq

  $sql9= select seq from `files` where item_id=.$POST[id]. and modul=.$modul. order by seq desc limit 1;

  echo $sql9.<br>;

  $res9=mysqli_query($db,$sql9);

  $r9=mysqli_fetch_assoc($res9);



  if(isset($r9[seq]) && ($r9[seq]>0)) {

    $seq=$r9[seq]+1;

  } else {

    $seq=1;

  }



  $folder_y=$ar[img_path].$modul./.date(y);

  if (!is_dir($folder_y)) {

    mkdir($folder_y, 0777, true);

  }

  $folder_ym=$folder_y./.date(m);

  if (!is_dir($folder_ym)) {

    mkdir($folder_ym, 0777, true);

  }



  $folder_ymd=$folder_ym./.date(d);

  if (!is_dir($folder_ymd)) {

    mkdir($folder_ymd, 0777, true);

  }



  $folder_ymdh=$folder_ymd./.date(H);

  if (!is_dir($folder_ymdh)) {

    mkdir($folder_ymdh, 0777, true);

  }



  $folder_main=$folder_ymdh./.$POST[id];

  if (!is_dir($folder_main)) {

    mkdir($folder_main, 0777, true);

  }

  $folder_seq=$folder_main./.$seq;

  if (!is_dir($folder_seq)) {

    mkdir($folder_seq, 0777, true);

  }

  $folder_tmp=$folder_seq./tmp;

  if (!is_dir($folder_tmp)) {

    mkdir($folder_tmp, 0777, true);

  }



  /*$sub_folder=$_POST[id]./.$seq./;

  switch($_POST[file_type]) {

    case Image;



  }*/



  $path=date(y)./.date(m)./.date(d)./.date(H)./.$POST[id];

  $e_ext=explode(/,$FILES[file][type]);

  $file_name=date(YmdHis)...$e_ext[1];



  if($POST[file_type]==image) {

    if (move_uploaded_file($FILES[file][tmp_name], $folder_tmp./. $FILES[file][name])) {

      //echo Image Card Front Uploaded;

      //$file_name=date(YmdHis)..jpg;





      $sub_folder=$folder_seq./image;

      $sub_folder_big=$folder_seq./image/big;

      $sub_folder_medium=$folder_seq./image/medium;

      $sub_folder_small=$folder_seq./image/small;



      if(!is_dir($sub_folder)){ mkdir($sub_folder, 0755); }

      if(!is_dir($sub_folder_big)){ mkdir($sub_folder_big, 0755); }

      if(!is_dir($sub_folder_medium)){ mkdir($sub_folder_medium, 0755); }

      if(!is_dir($sub_folder_small)){ mkdir($sub_folder_small, 0755); }



      $source_image=$folder_tmp./. $FILES[file][name];

      $img_big= $sub_folder_big./.$file_name;

      $img_medium= $sub_folder_medium./.$file_name;

      $img_small= $sub_folder_small./.$file_name;

      //$destination=$ar[img_path]..$img_url;

      //resize(640, $destination, $source_image);

      image_handler($source_image,$img_big,1080,1080,90,false);

      image_handler($source_image,$img_medium,800,800,90,false);

      image_handler($source_image,$img_small,150,150,90,false);



      echo $sub_folder_small.<br>;



      $udata2=array(

        id=>$iid,

        seq=>$seq,

        modul=>$modul,

        item_id=>$POST[id],

        type=>image,

        title=>gpc($title),

        caption=>gpc($caption),

        url_original=>tmp/.$FILES[file][name],

        create_at=>time(),

        path=>$path,

        url=>$file_name

        );

      Insert($db,files,$udata2,1);



      $udt2=array(

        img=>$modul./.$path./.$seq./image/small/.$file_name,

      );

      Update($db,$modul,$udt2,id=.$POST[id],1);

    }

  } else {

    if (move_uploaded_file($FILES[file][tmp_name], $folder_seq./.$file_name)) {

      //echo Image Card Front Uploaded;

      $udata2=array(

        id=>$iid,

        seq=>$seq,

        modul=>$modul,

        item_id=>$POST[id],

        type=>$POST[file_type],

        title=>gpc($title),

        caption=>gpc($caption),

        url_original=>tmp/.$FILES[file][name],

        create_at=>time(),

        path=>$path,

        url=>$file_name,

        );

      Insert($db,files,$udata2,0);

    }

  }   

}