<?php

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

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

$success = "";
$imagedb = "lrimage";
$vouchno = $_POST["vouchno"];
$source = $_POST["source"];
$myyear = $_SESSION[$myyear];
mysqli_select_db($conn, $zdb);


$path = "images/lr/".$myyear."/";

$fileName = "$source". "-" . $vouchno . ".jpg";
$img_file = $path . ($fileName);  

unlink($img_file);    

$fileTmpLoc = $_FILES["file1"]["tmp_name"]; // File in the PHP tmp folder
$fileType = $_FILES["file1"]["type"]; // The type of file it is
$fileSize = $_FILES["file1"]["size"]; // File size in bytes
$fileErrorMsg = $_FILES["file1"]["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 `$imagedb`")or die(mysqli_error($conn));
//    $gimg = mysqli_fetch_array($imgp);

//    mysqli_query($conn, "DELETE FROM `$imagedb` WHERE vouchno='$vouchno' AND `source`= '$source'") or die(mysqli_error($conn));

//    if ($gimg['vouchno'] == $vouchno) {
        mysqli_query($conn, "UPDATE `salemain` SET `imgpath`='$img_file' WHERE `vouchno`='$vouchno'") or die(mysqli_error($conn));
//    }

//    mysqli_query($conn, "INSERT INTO `$imagedb`(`vouchno`, `imgpath`, `source`)
//                            VALUES('$vouchno', '$img_file', '$source')") or die(mysqli_error($conn));
} else {
    echo "move_uploaded_file function failed";
}

?>