<?php
ob_start();
require("includes/check_auth_user.php");
$conn = mysqli_connect("localhost", "jazofaz_user", "rajF@1998") or die(mysqli_error());
mysqli_select_db($conn, "jazofaz_khoj_root") or die(mysqli_error($conn));

$zdbroot = $_SESSION["zdbroot"];
if (!empty($zdbroot)) {
    mysqli_select_db($conn, $zdbroot);
}
require("includes/function.php");

$zdir = $dbname;
$zcomp = 1;
$zdbroot = $zdir;
$focusfield = "submit";
$usertype = $_SESSION["usertype"];
$username = $_SESSION["username"];
$file = "default.php";

$zroot = $_SESSION["zdbroot"];
$zroot1 = substr($zroot, 0, -4);

//$zdb = $_SESSION["zdbyear_" . $dbname];
$zdb = $zroot1 . "1_1001";
mysqli_select_db($conn, $zdb);
//browse($zdb);
if (isset($_POST["submit"])) {

    $fileTmpLoc = $_FILES["file3"]["tmp_name"]; // File in the PHP tmp folder
    $fileType = $_FILES["file3"]["type"]; // The type of file it is
    $fileSize = $_FILES["file3"]["size"]; // File size in bytes
    $fileErrorMsg = $_FILES["file3"]["error"]; // 0 for false... and 1 for true    $filename = $file;
//    browse($fileTmpLoc);
//    browse($fileType);
//    browse($fileSize);
//    browse($fileErrorMsg);
    $fileName = 'database.sql';
    if (!empty($fileTmpLoc)) {

        if (move_uploaded_file($fileTmpLoc, "$fileName")) {
//             MySQL host
            $mysql_host = 'localhost';
// MySQL username
            $mysql_username = 'jazofaz_user';
// MySQL password
            $mysql_password = 'rajF@1998';
// Database name
            $mysql_database = $zdb;

            $mysqli = new mysqli("$mysql_host", "$mysql_username", "$mysql_password", "$mysql_database");
            $mysqli->query('SET foreign_key_checks = 0');
            if ($result = $mysqli->query("SHOW TABLES")) {
                while ($row = $result->fetch_array(MYSQLI_NUM)) {
                    $mysqli->query('DROP TABLE IF EXISTS ' . $row[0]);
                }
            }

            $mysqli->query('SET foreign_key_checks = 1');
            $mysqli->close();


// Connect to MySQL server
            $con = @new mysqli($mysql_host, $mysql_username, $mysql_password, $mysql_database);

// Check connection
            if ($con->connect_errno) {
                echo "Failed to connect to MySQL: " . $con->connect_errno;
                echo "<br/>Error: " . $con->connect_error;
            }

// Temporary variable, used to store current query
            $templine = '';
// Read in entire file
            $lines = file($fileName);
// Loop through each line
            foreach ($lines as $line) {
// Skip it if it's a comment
                if (substr($line, 0, 2) == '--' || $line == '')
                    continue;

// Add this line to the current segment
                $templine .= $line;
// If it has a semicolon at the end, it's the end of the query
                if (substr(trim($line), -1, 1) == ';') {
                    // Perform the query
                    $con->query($templine) or print('Error performing query \'<strong>' . $templine . '\': ' . $con->error() . '<br /><br />');
                    // Reset temp variable to empty
                    $templine = '';
                }
            }
            echo "<b style='font-size:25px'>Backup Upload Successfully</b>";
            $con->close($con);
        }
    } else {
        browse("Please Upload Correct File");
        header("refresh:1; $file");
    }
}
include("includes/linkshead.php");
?>
<body>
    <form action='' method='POST' enctype="multipart/form-data">
        <!-- Default box -->

        <div class="col-lg-5 col-md-5 col-sm-12 col-xs-12 pad">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><?php Echo $formname; ?></h3>
                </div>
                <div class="panel-body">
                    <div class="row">
                        <span class="login100-form-title p-b-37">
                            Select File
                        </span>
                        <div class="form-group has-feedback has-feedback-left text-center">
                            <input type="file" name="file3" id="file3" class="btn btn-primary" >
                        </div>
                    </div>
                    <div class="row">
                        <button class="login100-form-btn" type="submit" name="submit" id="submit">
                            Upload Data
                        </button>

                    </div>

                </div>
            </div>
        </div>
    </form>


    <?php include("includes/linksfoot.php")
    ?>
</body>

</html>