<?php
	session_start();
	include"../../connect/db.php"; 
 ?>  
<?php
	if( isset($_POST['email']) ){

		function deleteDir($path) {
		    if (empty($path)) { 
		        return false;
		    }
		    return is_file($path) ?
		            @unlink($path) :
		            array_map(__FUNCTION__, glob($path.'/*')) == @rmdir($path);
		}		

		$email = mysqli_real_escape_string($con,$_POST['email']);

		$path = "../../uploads/".$email."/";

		if(deleteDir($path)) {

			$target_dir = "../../uploads/".$email."/";
			mkdir($target_dir);
			$img_loc = "uploads/".$email."/";
			$target_file = $target_dir . basename($_FILES["profile-pic-update"]["name"]); 
			$img_loc_main = $img_loc . basename($_FILES["profile-pic-update"]["name"]);

			if (move_uploaded_file($_FILES["profile-pic-update"]["tmp_name"], $target_file)) {

				$sql = mysqli_query($con,"UPDATE users SET img_path = '$img_loc_main' WHERE email = '$email' ");

				if( $sql ){
					echo "success";
				}
				else{
					echo mysqli_error($con) ;
				}
			}
			else{
				echo "error";
			}	
		}
		else{
			echo "error";
		}


	}
	
?>