<?php
include '../db/db.php';
session_start();
$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){

  $valid_extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
  if(isset($_FILES['itm_img']))
    {

      /*if (isset($_POST['active'])) {
        $active = 1;
      }else{
        $active = 0;
      }*/

      //echo $active;
      $sub_title = htmlspecialchars(trim($_POST['sub_title']), ENT_QUOTES, 'UTF-8');
      $main_cat = htmlspecialchars(trim($_POST['main_cat']), ENT_QUOTES, 'UTF-8');
      $description = htmlspecialchars(trim($_POST['description']), ENT_QUOTES, 'UTF-8');


      $path = '../upload/export_banner/'; // upload directory
      $img = htmlspecialchars(trim($_FILES['itm_img']['name']), ENT_QUOTES, 'UTF-8');
      $tmp = htmlspecialchars(trim($_FILES['itm_img']['tmp_name']), 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).strtolower($img);
      
      // check's valid format
      if(in_array($ext, $valid_extensions))
      {
        $path = $path.strtolower($final_image); 
        //echo "img logo name".$final_image;
        if(move_uploaded_file($tmp,$path))
        {
          //echo "string";
          $sql = "INSERT INTO export_banner VALUES (NULL, '$final_image', '$sub_title', '$main_cat', '$description')";
          $result = mysqli_query($conn, $sql);
          if ($result) {
            echo "Data inserted";
          }
        }
      }
      else 
      {
        echo 'invalid';
      }
    }   


} else{
  $redirectUrl = "index.php";
  echo "<script type=\"text/javascript\">";
  echo "window.location.href = '$redirectUrl'";
  echo "</script>";

}}
?>	
