<?php
if(!function_exists("_")){
  function _($text){
    return $text;
  }
}

function generateName($lng, $file)
{
  $tmp = explode('.', $file);
  $end = end($tmp);

  $catalog = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
  $longString = strlen($catalog);
  $newString = "";
  $long = $lng;
  for ($i = 1; $i <= $long; $i++) {
    $pos = rand(0, $longString - 1);
    $newString .= substr($catalog, $pos, 1);
  }
  return $tmp[0]."-".$newString.".".$end;
}
$response_request = array();

$response_request["type"] = "error";
$response_request["title"] = "Error presented";
if($_FILES["file"]["name"]!=""){
  $tmp_name = $_FILES["file"]["tmp_name"];
  $name = generateName(4, basename($_FILES["file"]["name"]));
  if(move_uploaded_file($tmp_name, "../../uploads/docs/$name")){
    $response_request["type"] = "success";
    $response_request["name"] = basename($_FILES["file"]["name"]);
    $response_request["path"] = $name;
  }
}
echo json_encode($response_request);