<?php
header("Access-Control-Allow-Origin: * ");
header("Content-Type:application/json; charset=UTF-8");
header("Access-Control-Allow-Methods:POST");
header("Access-Control-Max-Age:3600");
header("Access-Control-Allow-Headers:Content-Type,Access-Control-Allow-Headers,Authorization,X-Requested-With");


$admin = isset($_GET['id'])?$_GET['id']:die();
include_once '../Database.php';
include_once 'User.php';

$database = new DataBase();
$db = $database->getConnection();
$usuario = new Usuario($db);


$path="upload/".$admin."/";
if(!file_exists($path)){
  //mkdir($dir,0775);
  mkdir($path,0777);
    
}
if(isset($_FILES['file'])){
	$originalname=$_FILES['file']['name'];
	$filePath = $path.$originalname;
	if (!is_writable($path)) {
		echo json_encode(array('status'=>false,'msg'=>'El directorio no existe, o no tiene permisos de acceso'));
	}
	if(move_uploaded_file($_FILES['file']['tmp_name'], $filePath)){
	    $query = "UPDATE ".$usuario->table_name." SET img=:foto where id=:id";
    	$stm = $usuario->conn->prepare($query);
    	$rut = "http://movil.dipresguerrero.com.mx/User/".$filePath;
    	$stm->bindParam(":foto",$rut);
    	$stm->bindParam(":id",$admin);
    	$stmt = $usuario->Insert($stm);
    	if($stmt !==false){
    		
		echo json_encode(array('status'=>true,'ruta'=>$rut));
    	}else{
    		echo json_encode(array('status'=> false,"msg"=>"No se ha creado el registro"));
    	}
	}
}else{
	echo json_encode(array('status'=> false,'msg'=>'No se ha cargado el archivo'));
}

?>