<?php
include 'dbconnection.php';

// Perform queries
$Email2 = $_POST["Email"]; 
$Id = $_POST["id"]; 


$DB = mysqli_query($mysql,"SELECT * FROM tb_users WHERE email ='$Email2'");
while($row = mysqli_fetch_array($DB)){
$dbname = $row ['dbname'];
$Documentosdir = $row ['Documentosdir'];
$Documentoshttp = $row ['Documentoshttp'];
}


$hostname2 = "localhost";
$username2 = "tempdpe5_nicolas";
$password2= "(Made7575)";
$conn = mysqli_connect($hostname2,$username2,$password2,$dbname);
if (mysqli_connect_errno()){
  printf("Connect failed: %s\n", mysqli_connect_error());
  exit();
} 
mysqli_set_charset($conn ,"utf8");


if (($_FILES['my_file']['name']!="")){
// Where the file is going to be stored
	$target_dir =$Documentosdir;
	$file = $_FILES['my_file']['name'];
	$path = pathinfo($file);
	$filename = $path['filename'];
	$ext = $path['extension'];
	$temp_name = $_FILES['my_file']['tmp_name'];
	$path_filename_ext = $target_dir.$filename.".".$ext;
	
	
mysqli_query($conn,"UPDATE comunicacion SET documento ='$file' WHERE ComID ='$Id'");	
 
// Check if file already exists
if (file_exists($path_filename_ext)) {
echo "<script>window.location.href='gestioncomunicacion';</script>";
 }else{
 move_uploaded_file($temp_name,$path_filename_ext);
echo "<script>window.location.href='gestioncomunicacion';</script>";
 }
}
?>
