<?php
header('Vary: Accept');
if (isset($_SERVER['HTTP_ACCEPT']) &&
    (strpos($_SERVER['HTTP_ACCEPT'], 'application/json') !== false)) {
    header('Content-type: application/json');
} else {
    header('Content-type: text/plain');
}
if(!isset($_POST["ruta"])){
    die;
}
$error = null;

$target_dir = "../news/".$_POST["ruta"]."/";
$target_file = $target_dir . basename($_FILES["file"]["name"]);

if (!file_exists($target_dir)) {
    mkdir($target_dir, 0775, true);
}

$imageFileType = pathinfo($target_file,PATHINFO_EXTENSION);
$check = getimagesize($_FILES["file"]["tmp_name"]);
if ($check == false) {
   $error = "Archivo NO es imagen";
}

if (!move_uploaded_file($_FILES["file"]["tmp_name"], $target_file)) {
    $error = "Se produjo un error al cargar el archivo";
}


$return = ['img' => $_FILES['file']['name'],'error' => $error,'reemplazo'=>$_POST["reemplazo"]==="true"];
echo json_encode($return);