<?php    
    if(isset($_POST['upload'])){
        $fileName = $_FILES['file']['name'];
        $fileTmpName = $_FILES['file']['tmp_name'];
        //find extension of file
        $fileExtension = pathinfo($fileName,PATHINFO_EXTENSION);
        //define allowed extension
        $allowedType = array('xls','xlsx','csv');
        if(!in_array($fileExtension, $allowedType)){
            echo'<div class="alert alert-danger absolue center text-center" role="alert">
                    <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                        <span aria-hidden="true">×</span>
                    </button>
                    <span class="text-danger">Invalide file extension....only excel documents of .xls .xlsx .csv extension is allowed</span>
                </div>';
        }else{
            $row=1;
            //upload file
            $handle = fopen($fileTmpName,'r');//read mode 'r'
            fgetcsv($handle, 1000, ",");
            while(($myData = fgetcsv($handle,1000,',')) !== FALSE){
               $row++;
               
                if($row>=10 && $myData[1] !=''){
                    echo $code = $myData[1];
                    $lec = $myData[2];
                    $uni = $myData[3];
                    $year = $myData[4];
                    $semister = $myData[5];
                    $category = $myData[6];
                    $mrks = $myData[7];
                    $stu = $myData[9];
                    $score = $myData[10];
                }
                /*
                if($category =="Practicals"){
                    $select = mysql_query("SELECT * FROM results WHERE code='$code'");
                    $counting=  mysql_num_rows($select);
                }else
                {
                    $select = mysql_query("SELECT * FROM results WHERE (code='$code') OR (unitID ='$uni' AND yosID='$year' AND semisterID='$semister' AND category='$category')");
                    $counting=  mysql_num_rows($select);
                }
                if($counting>0){
                    echo'<div class="alert alert-warning absolue center text-center" role="alert">
                            <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                <span aria-hidden="true">×</span>
                            </button>
                            <span class="text-warning">Results for this exam already exists already exists...Try downloainf another exam results sheet!</span>
                        </div>';
                }else{
                    if(!$insert = mysql_query("INSERT INTO results (category,code,total,added,unitID,studentID,marks,staffID) VALUES ('$category','$code','$mrks',NOW(),'$uni','$stu','$score','$staffID')"))
                    {
                        echo'<div class="alert alert-danger absolue center text-center" role="alert">
                                <button type="button" class="close" data-dismiss="alert" aria-label="Close">
                                    <span aria-hidden="true">×</span>
                                </button>
                                <span class="text-danger">'.mysql_error().'</span>
                            </div>';
                    }
                    else{
                        $_SESSION["new"]='True';
                        header('Location:exam-results.php');
                        echo "<script type='text/javascript'> document.location = 'exam-results.php'; </script>";
                        exit();
                    }
                }*/
            }
        }
    }
        
?>                                                             