<?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['files']))
    {

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

      $album_name = $id;

      $path = '../upload/albums/'.$album_name.'/'; // upload directory

      extract($_POST);
      $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).strtolower($img);
          
          // check's valid format
          if(in_array($ext, $valid_extensions))
          {
            $path = '../upload/albums/'.$album_name.'/';
            echo $path = $path.$final_image;
            //echo "img logo name".$final_image;
            if(move_uploaded_file($tmp,$path))
            {
              //echo "string";
              $sql = "INSERT INTO wedding_gallery VALUES (NULL, '$id', '$final_image' , '$album_name')";
              $result = mysqli_query($conn, $sql);
              if ($result) {
                echo "Data inserted";
              }
            }
          }
          else 
          {
            echo 'invalid';
          }
      }

    }

}
?>	
