<?php
include("include/connection.php");
 if($_POST['Save']!='')
 {
    if (isset($_FILES['bim'])) {
    $errors = array();
    $file_name = $_FILES['bim']['name'];
    $file_size = $_FILES['bim']['size'];
    $file_tmp = $_FILES['bim']['tmp_name'];
    $file_type = $_FILES['bim']['type'];
    // $file_ext = end(explode(".",$file_name));
    $tmp = explode('.', $file_name);
    $file_ext = end($tmp);
    $extensions = array("jpeg", "jpg", "png" ,"webp");
    // $query = mysqli_query($con, "select max(sid) as stid from student_details");
    // $result = mysqli_fetch_array($query);
    // $student_id = $result['stid'] + 1;
    // $dir = "images/student-images/$student_id";
    // mkdir($dir);
    if (in_array($file_ext, $extensions) === false) {
      $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
      header("location:gallary.php");
    }
    if ($file_size > 2097152) {
      $rerrors[] = "File size must be 2mb or lower.";
      $_SESSION['status'] = "File size must be 10mb or lower!";
      $_SESSION['status_code'] = "error";
      header("location:gallary.php");
    }
    if (empty($errors) == true) {
      move_uploaded_file($file_tmp, "../assets/images/gallary/". $file_name);
    } else {
      print_r($errors);
      header("location: gallary.php");
    }
  }    
    
     if (isset($_FILES['mbim'])) {
    $errors = array();
    $file_name1 = $_FILES['mbim']['name'];
    $file_size1 = $_FILES['mbim']['size'];
    $file_tmp1 = $_FILES['mbim']['tmp_name'];
    $file_type1 = $_FILES['mbim']['type'];
    // $file_ext = end(explode(".",$file_name));
    $tmp1 = explode('.', $file_name1);
    $file_ext1 = end($tmp1);
    $extensions1 = array("jpeg", "jpg", "png" ,"webp");
    // $query = mysqli_query($con, "select max(sid) as stid from student_details");
    // $result = mysqli_fetch_array($query);
    // $student_id = $result['stid'] + 1;
    // $dir = "images/student-images/$student_id";
    // mkdir($dir);
    if (in_array($file_ext1, $extensions1) === false) {
      $errors[] = "This extension file not allowed, Please choose a JPG or PNG file.";
      header("location:gallary.php");
    }
    if ($file_size1 > 2097152) {
      $rerrors[] = "File size must be 2mb or lower.";
      $_SESSION['status'] = "File size must be 10mb or lower!";
      $_SESSION['status_code'] = "error";
      header("location:gallary.php");
    }
    
    if (empty($errors) == true) {
      move_uploaded_file($file_tmp1, "../assets/images/gallary/". $file_name1);
    } else {
      print_r($errors);
      header("location: gallary.php");
    }
  }    
    $sq=mysqli_query($con,"insert into gallary set image_name='$file_name',pop_img='$file_name1'");
    if($sq){
        header("location:gallary.php");
    }
    
   // Configure upload directory and allowed file types
    // $upload_dir = '../assets/images/gallary/';
    //     $allowed_types = array('jpg', 'png', 'jpeg', 'gif','webp');
        //Define maxsize for files i.e 2MB
        // $maxsize = 2 * 1024 * 1024;
        //Checks if user sent an empty form
    //     if(!empty(array_filter($_FILES['files']['name']))) {
    //     // Loop through each file in files[] array
    //     foreach ($_FILES['files']['tmp_name'] as $key => $value) {
    //         $file_tmpname = $_FILES['files']['tmp_name'][$key];
    //         $file_name = $_FILES['files']['name'][$key];
    //         $file_size = $_FILES['files']['size'][$key];
    //         $file_ext = pathinfo($file_name, PATHINFO_EXTENSION);
    //         // Set upload file path
    //         $filepath = $upload_dir.$file_name;
    //         // Check file type is allowed or not
    //         if(in_array(strtolower($file_ext), $allowed_types)) {
    //             // Verify file size - 2MB max
    //             if ($file_size > $maxsize)        
    //                 echo "Error: File size is larger than the allowed limit.";
    //             // If file with name already exist then append time in
    //             // front of name of the file to avoid overwriting of file
    //             if(file_exists($filepath)) {
    //                 $filepath = $upload_dir.time().$file_name;
    //                 if( move_uploaded_file($file_tmpname, $filepath)) {
    //                     echo "{$file_name} successfully uploaded <br />";
    //                 }
    //                 else {                    
    //                     echo "Error uploading {$file_name} <br />";
    //                 }
    //             }
    //             else {
                 
    //                 if( move_uploaded_file($file_tmpname, $filepath)) {
    //                     echo "{$file_name} successfully uploaded <br />";
    //                     $sq=mysqli_query($con,"insert into gallary set image_name='$file_name'");
    //                     header("location:gallary.php");
    //                 }
    //                 else {                    
    //                     echo "Error uploading {$file_name} <br />";
    //                      }
    //                 }
    //             }
    //         else {
    //             // If file extension not valid
    //             echo "Error uploading {$file_name} ";
    //             echo "({$file_ext} file type is not allowed)<br / >";
    //         }
    //     }
    // }
    // else {
        // If no files selected
    //     echo "No files selected.";
    // }
		
 }
 
?>
<!DOCTYPE html>
<html lang="en">
<head>
   <?php include("element/css_js.php"); ?>
   <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
</head>
<body class="hold-transition dark-mode sidebar-mini layout-fixed layout-navbar-fixed layout-footer-fixed">
<div class="wrapper">
  <!-- Preloader -->
   <?php include("element/header.php"); ?>
  <!-- Main Sidebar Container -->
<?php include("element/menu.php"); ?>
  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <div class="container-fluid">
        <div class="row mb-2">
          <div class="col-sm-6">
            <h1>Manage Gallary</h1>
          </div>
          <div class="col-sm-6">
            <ol class="breadcrumb float-sm-right">
              <li class="breadcrumb-item"><a href="#">Home</a></li>
              <li class="breadcrumb-item active"> Menu</li>
            </ol>
          </div>
        </div>
      </div><!-- /.container-fluid -->
    </section>
    <!-- Main content -->
    <section class="content">
      <div class="container-fluid">
        <div class="row">
          <!-- left column -->
          <div class="col-md-12">
            <!-- general form elements -->
            <div class="card card-primary">
              <div class="card-header">
                <h3 class="card-title"> Gallary</h3>
              </div>
              <!-- /.card-header -->
              <!-- form start -->
              <form method="post" enctype="multipart/form-data">
                <div class="card-body">
                    <div class="row">
                     <div class="col-md-9 mx-auto">
                           <div class="form-group">
                                <label for="exampleInputEmail1">Image</label>
                                <input type="file"  name="bim" id="gallery-photo-add" class="form-control">
                                
                            </div>
                            <div class="form-group">
                                <label for="exampleInputEmail1">Popup Image</label>
                                <input type="file"  name="mbim" id="" class="form-control">
                            </div>
                            
                          <div class="form-group">
                            <button type="submit" name="Save" value="Save" class="mt-2 btn btn-primary">Save</button>
                         </div>
                     </div>
                <!-- /.card-body --></div>
                    </div>
              </form>
              


            </div></div></div></div></section></div>
  <!-- /.content-wrapper -->
  <footer class="main-footer">
    <strong>Copyright &copy; 2021-2022 <a href="#">koscove Admin</a>.</strong>
    All rights reserved.
    <div class="float-right d-none d-sm-inline-block">
     <!-- <b>Version</b> 3.1.0-->
    </div>
  </footer>
  <!-- Control Sidebar -->
  <aside class="control-sidebar control-sidebar-dark">
    <!-- Control sidebar content goes here -->
  </aside>
  <!-- /.control-sidebar -->
</div>
<!-- ./wrapper -->
<!-- jQuery -->
<script src="plugins/jquery/jquery.min.js"></script>
<!-- jQuery UI 1.11.4 -->
<script src="plugins/jquery-ui/jquery-ui.min.js"></script>
<!-- Resolve conflict in jQuery UI tooltip with Bootstrap tooltip -->
<script>
  $.widget.bridge('uibutton', $.ui.button)
</script>
<!-- Bootstrap 4 -->
<script src="plugins/bootstrap/js/bootstrap.bundle.min.js"></script>
<!-- ChartJS -->
<script src="plugins/chart.js/Chart.min.js"></script>
<!-- Sparkline -->
<script src="plugins/sparklines/sparkline.js"></script>
<!-- JQVMap -->
<script src="plugins/jqvmap/jquery.vmap.min.js"></script>
<script src="plugins/jqvmap/maps/jquery.vmap.usa.js"></script>
<!-- jQuery Knob Chart -->
<script src="plugins/jquery-knob/jquery.knob.min.js"></script>
<!-- daterangepicker -->
<script src="plugins/moment/moment.min.js"></script>
<script src="plugins/daterangepicker/daterangepicker.js"></script>
<!-- Tempusdominus Bootstrap 4 -->
<script src="plugins/tempusdominus-bootstrap-4/js/tempusdominus-bootstrap-4.min.js"></script>
<!-- Summernote -->
<script src="plugins/summernote/summernote-bs4.min.js"></script>
<!-- overlayScrollbars -->
<script src="plugins/overlayScrollbars/js/jquery.overlayScrollbars.min.js"></script>
<!-- AdminLTE App -->
<script src="dist/js/adminlte.js"></script>
<!-- AdminLTE for demo purposes -->
<script src="dist/js/demo.js"></script>
<!-- AdminLTE dashboard demo (This is only for demo purposes) -->
<script src="dist/js/pages/dashboard.js"></script>
<script>$(function() {
    // Multiple images preview in browser
    var imagesPreview = function(input, placeToInsertImagePreview) {

        if (input.files) {
            var filesAmount = input.files.length;

            for (i = 0; i < filesAmount; i++) {
                var reader = new FileReader();

                reader.onload = function(event) {
                    $($.parseHTML('<img>')).attr('src', event.target.result).appendTo(placeToInsertImagePreview);
                }

                reader.readAsDataURL(input.files[i]);
            }
        }

    };

    $('#gallery-photo-add').on('change', function() {
        imagesPreview(this, 'div.gallery');
    });
});</script>
</body>
</html>