<?php
include("../commonpages/dbconnect.php");

if(isset($_POST['save'])){
	$action=$_POST['image_action'];
	 $oldimage=$_POST['oldimage'];
	$id=$_POST['id'];
	
	if($action=="profile")
	{
					
					if($oldimage==""){
					$newimage=$_FILES['image']['name'];
					$file_name = $_FILES['image']['name'];
					$file_tmp =$_FILES['image']['tmp_name'];
				    $target_dir="../images/profile_pic/";
					$file_ext = substr($file_name, strripos($file_name, '.')); // get file name 
					$file_name="profile".rand(4500,9050).'_'.$id.$file_ext;
					$finalfile=$target_dir.$file_name;
						
					move_uploaded_file($file_tmp,$finalfile);
					
				
				    $sql="UPDATE user_registration SET profile_pic='$file_name' WHERE id=$id";
			        $data=mysqli_query($con,$sql);
					header("Location:../image-change.php?action=profile");
		}
		else{
					$file_name=$_FILES['image']['name'];
					//echo"<br>";
					$oldimage=($_REQUEST['oldimage']);
					$file_tmp =$_FILES['image']['tmp_name'];
					
					move_uploaded_file($file_tmp,"../images/profile_pic/".$oldimage);
				
					$sql="UPDATE user_registration SET profile_pic='$oldimage' WHERE id=$id";
					$data=mysqli_query($con,$sql);
					header("Location:../image-change.php?action=profile");
			
		}
		
	}
	else{
		if($oldimage==""){
					$newimage=$_FILES['image']['name'];
					$file_name = $_FILES['image']['name'];
					$file_tmp =$_FILES['image']['tmp_name'];
				    $target_dir="../images/cover_pic/";
					$file_ext = substr($file_name, strripos($file_name, '.')); // get file name 
					$file_name="cover".rand(4500,9050).'_'.$id.$file_ext;
					$finalfile=$target_dir.$file_name;
						
					move_uploaded_file($file_tmp,$finalfile);
					
				
				    $sql="UPDATE user_registration SET cover_pic='$file_name' WHERE id=$id";
			        $data=mysqli_query($con,$sql);
					header("Location:../image-change.php?action=cover");
		}
		else{
					$file_name=$_FILES['image']['name'];
					//echo"<br>";
					$oldimage=($_REQUEST['oldimage']);
					$file_tmp =$_FILES['image']['tmp_name'];
					
					move_uploaded_file($file_tmp,"../images/cover_pic/".$oldimage);
				
					$sql="UPDATE user_registration SET cover_pic='$oldimage' WHERE id=$id";
					$data=mysqli_query($con,$sql);
					header("Location:../image-change.php?action=cover");
			
		}
		
	}
}
?>