<?php
session_start();
require_once "main.php";
Logger::configure('log4php.xml');
$log = Logger::getLogger('uploadreads');
global $db;
if ($_SESSION['access']<3){
    $zid = 'reads';
    $i=0;
    $sql = 'INSERT INTO mtemp ( id, company, zid, xread, batch ) VALUES ';
    $comz = $_SESSION['comnum'];
    $batchz = $_POST['batchid'];
    if(isset($_FILES["csvfile"])){
        if(($cs = fopen($_FILES["csvfile"]["tmp_name"],"r")) !==FALSE) {
            while(($data = fgetcsv($cs, 1000, ","))){
                if($i>0){
            $sql = $sql . '( NULL, "' . $comz . '", "'. $data[0] . '", "' . $data[7] . '", "' . $batchz . '" ),';
                }
                $i++;
            }
       echo $sql;
       $sql = rtrim($sql, ",");
       $st = $db->prepare($sql);
       $st->execute();
       $log->info( (!empty($_GET['submitType']) ? $_GET['submitType'] : null).' - user = '.$_SESSION['user'].' - '.$_SESSION['comnum']);
       echo "The file ".  basename( $_FILES['csvfile']['name']). " has been uploaded.  Please refresh the browser and then go back to reads.";
    }
    }
} else {

?>
    <script>alert('You do not have a high enough access level to upload files!\n\
                   Please see your administrator.');
    </script>
<?php
}
////echo 'File: <b>' . $fileName . '</b> was uploaded succesfully.<br>';
?>
