<?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{

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

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

      //echo $active;


      $title=htmlspecialchars(trim($_POST['title']), ENT_QUOTES, 'UTF-8');


      //mkdir('../upload/albums/'.$album_count);
      $path = '../upload/our_nursery/'; // 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).$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 our_nursery VALUES (NULL, '$title', '$final_image')";
          $result = mysqli_query($conn, $sql);
          if ($result) {
            echo "Data inserted";
          }
        }
      }
      else 
      {
        echo 'invalid';
      }
      
    }

    if (array_key_exists('del_our_nur', $_POST)) {
      $id=htmlspecialchars(trim($_POST['id']), ENT_QUOTES, 'UTF-8');

      $sql2=mysqli_query($conn,"DELETE FROM our_nursery WHERE id = '$id'");
    }   


}
?>	
