
<?php  
	require "commonincludefiles.php";
	function copy_directory( $source, $destination) {
	
	// to blank directory of destination first
    foreach(glob($destination.'*.*') as $v){
    unlink($v);
    }
	
	if (is_dir( $source )) {
		@mkdir( $destination );
		
		$directory = dir( $source );
		while ( FALSE !== ( $readdirectory = $directory->read() ) ) {
			if ( $readdirectory == '.' || $readdirectory == '..' ) {
				continue;
			}
			$PathDir = $source . '/' . $readdirectory; 
			if ( is_dir( $PathDir ) ) {
				echo $PathDir;
				copy_directory( $PathDir, $destination . '/' . $readdirectory );
				continue;
			}
			else
			{
			 $Ext = substr($readdirectory, strrpos($readdirectory , ".")+1);
             $Ext = strtolower($Ext);
			 
			  if (preg_match("/(pdf)/",$Ext))	
			  {	
				copy( $PathDir, $destination . '/' . $readdirectory );
			  }
			}
		}
 
		$directory->close();
	}else {
		copy( $source, $destination );
	}
	}

	if($_SESSION["VedantnrUserId"]!="" && isset($_SESSION["VedantnrUserId"]))
	{
		$sourcedir=$_POST['uploadfile'];
		
		$droot="./gtu result pdfs/";
		
		if(!is_dir($droot))
			{
			mkdir($droot, 0755); //0777 full access to specified file
			chmod($droot, 0755);//change the mode of the specified file to that given in mode. 
			}
		
		$dyearpath=$droot.$_SESSION['aYearId']."/";
		if(!is_dir($dyearpath))
			{
			mkdir($dyearpath, 0755); //0777 full access to specified file
			chmod($dyearpath, 0755);//change the mode of the specified file to that given in mode. 
			}
			
		$dbranchpath=$dyearpath.$_POST['branch_hidden']."/";
		if(!is_dir($dbranchpath))
			{
			mkdir($dbranchpath, 0755); //0777 full access to specified file
			chmod($dbranchpath, 0755);//change the mode of the specified file to that given in mode. 
			}
			
		$dsempath=$dbranchpath.$_POST['semester_hidden']."/";
		if(!is_dir($dsempath))
			{
			mkdir($dsempath, 0755); //0777 full access to specified file
			chmod($dsempath, 0755);//change the mode of the specified file to that given in mode. 
			}
			
		$dresulttypepath=$dsempath.$_POST['resulttype']."/";
		if(!is_dir($dresulttypepath))
			{
			mkdir($dresulttypepath, 0755); //0777 full access to specified file
			chmod($dresulttypepath, 0755);//change the mode of the specified file to that given in mode. 
			}
			
		$destpath=$dresulttypepath;
		 
		copy_directory($sourcedir,$destpath);
		
		echo '{"success":"true"}';
	}
	else
	{
		echo '{"success":"false","msg":"session expiry"}';
	}
?>