<?php
require '../connForms/acdet.php';

$userName = $_POST['userName'];

$response = "no answer";

$mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

$userName = $mysqli->real_escape_string($userName);

//path to upload directory
$dirPath = "../shapeDetect/".$userName."/myCustomXML/";

//check to see if username is in database
$sqlName = "SELECT * FROM accounts WHERE username = '$userName'";
$resultName = $mysqli->query($sqlName);

if($resultName -> num_rows > 0){

    //function to upload xml file
    function uploadXML($dir) {
        $file_tmp = $_FILES['picker']['tmp_name'];
        $file_name = $_FILES['picker']['name'];
        $file_size = $_FILES['picker']['size'];
        $ext = explode( ".", $file_name );
        $ext = $ext[1];

        if ( $ext != 'xml' ) {
            return "Only XML files allowed";

        } else if(file_exists($dir.$file_name)) {
           return $file_name . " already exists";

        } else {
            if ( is_uploaded_file( $file_tmp ) ) {
                move_uploaded_file( $file_tmp, $dir . $file_name );
               return "File uploaded!";
            } else {
                return "There was a porblem while uploading!";
            }
        }
    }

    $response = uploadXML($dirPath);

} else {
    $response = $userName." not found in database. Possible connection error!";
}


echo $response;

$mysqli->close();

?>