<?php

    include '../db/db.php';

    

    $log=htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');

    if (empty($log)) {

         

      $redirectUrl = "index.php";

      echo "<script type=\"text/javascript\">";

      echo "window.location.href = '$redirectUrl'";

      echo "</script>";

         

    }else{



      $sqlu="SELECT * FROM details_of_users WHERE user='$log' and val='1'";

      $resultu=mysqli_query($conn, $sqlu);

      $countu=mysqli_num_rows($resultu);

      

    if($countu==1){



      $sqlc="SELECT * FROM details_of_users where user='$log'";

      $resultc=mysqli_query($conn, $sqlc);

      while ($rowc = mysqli_fetch_array($resultc)){

      $name=$rowc['id'];

    }



    



  $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');

  if(isset($_FILES['files']))

    {



      /*if (isset($_POST['active'])) {

        $active = 1;

      }else{

        $active = 0;

      }*/



      //echo $active;



      $id=htmlspecialchars(trim($_GET['id']), ENT_QUOTES, 'UTF-8');



/*      echo $sql_count="SELECT item_code FROM items WHERE id = '$id'";

      $result_count=mysqli_query($conn, $sql_count);

      $row_count= mysqli_fetch_array($result_count);

      print_r($row_count);

      echo $album_name = $row_count['item_code'];*/



      $path = '../upload/item/'.$id.'/'; // upload directory



      extract($_POST);

      $result_ver = 'FALES';

      $error=array();

      $extension=array("jpeg","jpg","png","gif");

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

      {

          $final_image = '';

          $img = '';

          $tmp = '';

          $img = htmlspecialchars(trim($_FILES['files']['name'][$key]), ENT_QUOTES, 'UTF-8');

          $tmp = htmlspecialchars(trim($_FILES['files']['tmp_name'][$key]), ENT_QUOTES, 'UTF-8');

            

          // get uploaded file's extension

          $ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));

          

          // can upload same image using rand function

          $final_image = rand(1000,1000000).rand(1,1000000).$img;

          

          // check's valid format

          if(in_array($ext, $valid_extensions))

          {

            $path = '../upload/item/'.$id.'/';

            $path = $path.strtolower($final_image);

            //echo "img logo name".$final_image;

            if(move_uploaded_file($tmp,$path))

            {

              //echo "string";

              $sql = "INSERT INTO gallery VALUES (NULL, '$id', '$final_image' , '$id')";

              $result = mysqli_query($conn, $sql);

              if ($result) {

                $result_ver = 'TRUE';

              }

            }

          }

          else 

          {

            $result_ver = 'FALES';

            break;
          }

      }

      if ($result_ver == 'TRUE') {
        echo "Data inserted";
      }else{
        echo 'invalid ';
      }

    }






} else{

  $redirectUrl = "index.php";

  echo "<script type=\"text/javascript\">";

  echo "window.location.href = '$redirectUrl'";

  echo "</script>";



}}

?>	

