<?php
require("includes/check_auth_user.php");
require("includes/dbconnect.php");
include('includes/function.php');
include('includes/simgeImage.php');

$zdb    = $_SESSION["zdbyear_" . $dbname];
$zyear  = right($zdb,4);

mysqli_select_db($conn,$zdb);
$success    = "";
$vouchno    = $_POST["evouchno"];
$passno     = $_POST["epassno"];
$lastname   = $_POST["mlastname"];
$name       = $_POST["mname"];
$path       = "../tlt/admin/zip/";

$fileName    =  $vouchno . ".zip";
$img_file    = $path. ($fileName);

//$fileName       = $_FILES["file1"]["name"]; // The file name
$fileTmpLoc     = $_FILES["file2"]["tmp_name"]; // File in the PHP tmp folder
$fileType       = $_FILES["file2"]["type"]; // The type of file it is
$fileSize       = $_FILES["file2"]["size"]; // File size in bytes
$fileErrorMsg   = $_FILES["file2"]["error"]; // 0 for false... and 1 for true
if (!$fileTmpLoc) { // if file not chosen
    echo "ERROR: Please browse for a file before clicking the upload button.";
    exit();
}
if (move_uploaded_file($fileTmpLoc, "$img_file")) {
    
    echo "$fileName upload is complete";

    ###########################SAVE PART################################################   
    $imgp = mysqli_query($conn,"SELECT * FROM `leadzip`")or die(mysqli_error($conn));
    $gimg = mysqli_fetch_array($imgp);

    mysqli_query($conn,"DELETE FROM `leadzip` WHERE vouchno='$vouchno'") or die(mysqli_error($conn));

    if ($gimg[vouchno] == $vouchno) {
        mysqli_query($conn,"UPDATE `leadzip` SET `vouchno`='$vouchno', `zippath`='$img_file', `name`='$name', `lastname`='$lastname' WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
    }

    mysqli_query($conn,"INSERT INTO `leadzip`(`vouchno`, `zippath`, `name`, `lastname`)
                            VALUES('$vouchno', '$img_file', '$name', '$lastname')") or die(mysqli_error($conn));
} else {
    echo "move_uploaded_file function failed";
}

?>