<?php 


ob_start(); session_start();?>
<?php
    
if(!isset($_SESSION['tfbs'])){
    header("Location: index.php");
}else if($_SESSION['tfbs'] == null){
    header("Location: index.php");
   
    
}else{
     $cname = $_SESSION['tfbs'];
}

?>
<?php



function checkfilestatus($fname, $ftemp, $fsize){
   
$totall = array();    
$result="";
$target_dir = "../imgs/";
$target_file = $target_dir . basename($fname);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);  
    
 // Check if image file is a actual image or fake image
 $check = (null != $ftemp) ? getimagesize($ftemp) : false;
    if($check !== false) {

         if ($fsize > 500000) {
//                $result= "Sorry, your file is too large.";
                $result= "حجم فایل بیشتر از 500 کیلوبایت می باشد";
                $uploadOk = 0;
            }else{
                
                if($imageFileType != "jpg" && $imageFileType != "png" && $imageFileType != "jpeg"
                    && $imageFileType != "gif" ) {
                       // $result= "Sorry, only JPG, JPEG, PNG & GIF files are allowed.";
                        $result= "تنها عکس با فرمت های JPG، PNG، مورد قبول می باشند.";
                        $uploadOk = 0;
                    }
                
            }
         
        
        
    } else {
        $result= (null != $ftemp) ? "فایل ارسال شده عکس نمی باشد" : "فایلی ارسال نشده است";
        $uploadOk =  (null != $ftemp) ? 0 : 100;
    }
    
    
    
    
     $totall = array($uploadOk, $result,$fname);
    return $totall;
    
}


function getitrightornotImage($namefor, $fname, $ftemp, $fsize){
   
$totall = array();    
$result="";
$target_dir = "../imgs/";
$target_file = $target_dir . basename($fname);
$uploadOk = 1;
$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);  
    
 
                    
                    $tempz = explode(".", $fname);
                    $target_file = $namefor . '_SABA_'.$fname;                    
                    if (move_uploaded_file($ftemp, $target_dir . $target_file)) {
                        $uploadOk = 1;
                        $result= "با موفقیت ثبت گردید";
                    } else {
                         $uploadOk = 0;
                         $result= "انتقال فایل با مشکل روبرو شد";
                    }
    
    
    
    
    $totall = array($uploadOk, $result,$fname);
    return $totall;
    
}

if(isset($_POST['getsomepic'])){
    
    $errlist = array();
    $oklist = array();
    $addpics=array();
    if(isset($_FILES["photos"]))  
        { 
        
            // Retrieving each selected option 
 

                for($i=0; $i<sizeof($_FILES['photos']['name']); $i++){
                    
                     array_push($addpics, array("name" => $_FILES['photos']['name'][$i], "tmp_name" => $_FILES['photos']['tmp_name'][$i], "size" => $_FILES['photos']['size'][$i]));
                    
                }
        
        
                if(sizeof($addpics) > 0){  
        
                 for($i=0; $i<sizeof($addpics); $i++){
                      
                     $tempw = $addpics[$i];
                     
                     $saba_image_temp = $tempw['tmp_name'];
                     $saba_image_temp_image_size = $tempw['size'];
                     $saba_image_temp_image_name = $tempw['name'];
                     
                     if(null != $saba_image_temp_image_size){
                         
                         $temp = checkfilestatus($saba_image_temp_image_name,$saba_image_temp,$saba_image_temp_image_size); 
            
                         if($temp[0] === 0){
                            array_push($errlist, array($temp[2],$temp[1]));
                          }
                         
                     }
                     
                    }
                    
                    
                    if(sizeof($errlist) === 0){
                        
                        
                       for($i=0; $i<sizeof($addpics); $i++){
                      
                             $tempw = $addpics[$i];
                     
                             $saba_image_temp = $tempw['tmp_name'];
                             $saba_image_temp_image_size = $tempw['size'];
                             $saba_image_temp_image_name = $tempw['name'];
                     
                             if(null != $saba_image_temp_image_size){
                         
                                 $temp = getitrightornotImage($cname,$saba_image_temp_image_name,$saba_image_temp,$saba_image_temp_image_size); 
                                 
                                 
                                 array_push($oklist, array($temp[2],$temp[1]));
                           
                             }
                     
                          }
                        
                    }else{
                       
                    }
        
        
                }else{
                    
                }
               
        }

}

?>


<!doctype html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
    <link rel="stylesheet" href="https://fonts.googleapis.com/icon?family=Material+Icons">
    <link rel="stylesheet" href="css/design.css">
    <link rel="stylesheet" href="css/image-uploader.min.css">
    <title>سامانه بارگذاری عکس صبا</title>

</head>

<body>
    <div class="container-fluid ">

        <nav class="navbar navbar-light bg-white border-bottom border-light">

            <form class="form-inline" action="index.php">
                <button class="btn btn-outline-success my-2 my-sm-0" type="submit">خروج</button>
            </form>
            <a class="navbar-brand">سامانه بارگذاری عکس صبا <img src="http://www.shirazsaba.com/logo.png" width="30" height="30" class="d-inline-block align-top mx-2" alt=""></a>
        </nav>




        <div class="row mt-5">

            <div class="col-12 text-right mb-2" dir="rtl">

                <div class="h5 mx-3">به سامانه بارگذاری عکس صبا خوش آمدید.</div>
                <div class="h6 mx-3">لطفا عکس محصولات خود را در پنجره پایین بارگذاری کرده و سپس بر روی دکمه انتقال کلیک کنید.</div>

            </div>


            <div class="col-12">

                <form method="post" name="uploadimgform" action="" id="uploadimgform" enctype="multipart/form-data">


                    <div class="input-field"  style="cursor: pointer;">
                        <div class="input-images" style="padding-top: .5rem;"></div>
                    </div>

                    <button name="getsomepic">انتقال تصاویر</button>

                </form>

            </div>


            <div class="col-12 my-2">

              <div class="row mx-3 text-right">
                  
                <?php
                  
                   if(sizeof($errlist) > 0){
                       
                        echo ' <div class="col-12 h6 mx-3 mt-3">لیست خطاها</div></br>';
                       
                    
                  
                  
                    for($i=0; $i<sizeof($errlist); $i++){
            
                      $tempz = $errlist[$i];
            
                      echo ' <div class="col-12 h6 mx-3"><div class=" p-2  text-right text-dark rounded my-1 font-weight-normal" style="background-color:#f8f8f8;">'.$tempz[1].' -- ('.$tempz[0].')</div></div>';
            
            
                    }
                  
                  }else if(sizeof($oklist) > 0){
                       
                       
                      echo ' <div class="col-12 h6 mx-3 mt-3">گزارش انتقال فایل ها</div></br>';
                       
                    
                  
                  
                    for($i=0; $i<sizeof($oklist); $i++){
            
                      $tempz = $oklist[$i];
            
                      echo ' <div class="col-12 h6 mx-3"><div class=" p-2  text-right text-success rounded my-1 font-weight-normal" style="background-color: #50ce7d29;">'.$tempz[1].' -- ('.$tempz[0].')</div></div>';
            
            
                    }                       
                       
                       
                       
                   }
                
                ?>
                  
                  
              </div>
               

               
               

            </div>


        </div>

    </div>

    <!-- Optional JavaScript -->
    <!-- jQuery first, then Popper.js, then Bootstrap JS -->
    <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.0/dist/umd/popper.min.js" integrity="sha384-Q6E9RHvbIyZFJoft+2mJbHaEWldlvI9IOYy5n3zV9zzTtmI3UksdQRVvoxMfooAo" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/js/bootstrap.min.js" integrity="sha384-wfSDF2E50Y2D1uUdj0O3uMBJnjuUD4Ih7YwaYd1iqfktj0Uod8GCExl3Og8ifwB6" crossorigin="anonymous"></script>
    <script src="css/image-uploader.min.js"></script>

    <script>
        $(document).ready(function() {

            $('.input-images').imageUploader({
                imagesInputName: 'photos'
            });

        });
    </script>


</body></html>