<?php
session_start();
include_once "../../config.php";
include_once "../database1.php";
include_once "../../validation.php";
include_once "../../Helper.php";

require_once "../check_admin.php";


if(!isset($_FILES["file"]["name"]))
{
	http_response_code(404);
	echo json_encode(array('error_text' => 'File Not Found'));
	exit;
}

$target_dir = "../../uploads/geolite/";
$newName = 'ip.mmdb';
$target_file = $target_dir . $newName;
$message = '';



$extArray = explode(".", $_FILES["file"]["name"]); # extra () to prevent notice
$imageFileType = end($extArray);


// Check file size
if ($_FILES["file"]["size"] > 500000000) {
    $message.="Sorry, your file is too large.";
}
// Allow certain file formats
if($imageFileType != "mmdb") {
    $message.="Sorry, only MMDB files are allowed.";
}
// Check if $uploadOk is set to 0 by an error
if (trim($message) != '') {
    http_response_code(400);
    echo json_encode(['error_text' => $message]);
    exit;
// if everything is ok, try to upload file
} else {
	
	$fileError = '';
	$count = 0;
    $tmpName = $_FILES['file']['tmp_name'];
 	
 	if(move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
 		http_response_code(200);
        echo json_encode(['message' => 'Database Updated Successfully']);
        exit;

 	}
 	else
 	{
 		http_response_code(500);
        echo json_encode(['error_text' => 'Something Went Wrong']);
        exit;
 	}
 

    
  }

