0xV3NOMx
Linux ip-172-26-7-228 5.4.0-1103-aws #111~18.04.1-Ubuntu SMP Tue May 23 20:04:10 UTC 2023 x86_64



Your IP : 18.191.192.250


Current Path : /proc/thread-self/root/var/www/html/gfgcg/src/
Upload File :
Current File : //proc/thread-self/root/var/www/html/gfgcg/src/admuploadexsheet_07082019.php

<?php

function replaceChars($name)
{
	$spl_char = array("*","/","~","!","@","#","$","%","^","&",":",";","?","/","\\","_","-","'"," ","");
	$link_name = strtolower(str_replace($spl_char,"_",$name));
	return str_ireplace("__","_",$link_name);
}


function admuploadexeclsheet($aobj_context)
{
	$aobj_context->mobj_db->SetFetchMode(ADODB_FETCH_ASSOC); 
	session_start();
	require_once dirname(__FILE__) . '/../PHPExcel/Classes/PHPExcel/IOFactory.php';

	$date = date("dmYhis");
	$collcode = $_SESSION['collcode'];
	$file_name=replaceChars(basename($_FILES['admuploadsheet']['name']));
	$path_arr=pathinfo($_FILES['admuploadsheet']['name']);
	$file_ext=$path_arr['extension'];
	//var_dump($file_ext);
	$folderpath = $aobj_context->main_src."/admupload/"; 
	$temp_path = $aobj_context->main_src."/admupload/";
	if(!file_exists($temp_path))
	{
		mkdir($temp_path);
	}

	$file_path="/{$collcode}_{$date}_{$file_name}";
	$temp_path.=$file_path;
	
	if($collcode == '')
	{
		$data = "Please login and try again";
		echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
		return;
	}	
	
	if(isset($_FILES['admuploadsheet']['name']) && $_FILES['admuploadsheet']['name'] != '')  
	{ 
		$size=($_FILES['admuploadsheet']['size']/1024);
		
		if($size>1024)
		{
			$data="Please Upload file of size less than 1 MB";
			echo $aobj_context->mobj_output->ToJSONEnvelope($data,-2,"failure"); 
			return;
		}
		
		$ext = pathinfo($_FILES['admuploadsheet']['name'], PATHINFO_EXTENSION);

		if($ext != 'xlsx')
		{
			$data="Please Upload file xlsx format only";
			echo $aobj_context->mobj_output->ToJSONEnvelope($data,-2,"failure"); 
			return;
		}

		if(move_uploaded_file($_FILES['admuploadsheet']['tmp_name'],$temp_path))
		{
			$data="admupload{$file_path}";

			$query = "insert into admfile(fcollcode,ffilename,fuploadtime) 
			value ('{$collcode}','{$data}',now())";
			$results = $aobj_context->mobj_db->Execute($query);
			
			if(!$results)
			{
				$data = "Upload failure. Error inserting uploaded file";
				echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
				return;
			}

			if(!file_exists($temp_path))
			{
				$data = "Upload failure. Upload file does not exist";
				echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
				return;
			}
			$inputFileName = $temp_path;
			$inputFileType = 'Excel2007';
			///////////////////// Code To convert xlsx to csv \\\\\\\\\\\\\
			$objReader = PHPExcel_IOFactory::createReader($inputFileType);
			$objPHPExcelReader = $objReader->load($inputFileName);

			$loadedSheetNames = $objPHPExcelReader->getSheetNames();

			$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcelReader, 'CSV');
			$i = 0;
			foreach($loadedSheetNames as $sheetIndex => $loadedSheetName) 
			{
				if($i != 0)
					break;
			    $objWriter->setSheetIndex($sheetIndex);
				$csvFile = $folderpath.$date.$loadedSheetName.'.csv';
				$objWriter->setUseBOM(true);
			    $objWriter->save($csvFile);
			    $query = "LOAD DATA LOCAL INFILE '{$csvFile}'
		        INTO TABLE tmpcollstud
		        FIELDS TERMINATED BY \",\"
		        OPTIONALLY ENCLOSED BY '\"' 
		        LINES TERMINATED BY \"\n\" 
		        IGNORE 1 LINES
		        (@col1,@col2,@col3,@col4,@col5,@col6) set
		        fslno=@col1, fcollcode='{$collcode}', fdegree=@col3,
		        fname=@col4, fmobileno=@col5, femail=@col6";
				// var_dump($query);    
				$results = $aobj_context->mobj_db->Execute($query);
				// var_dump($results);
				$rows_affected = $aobj_context->mobj_db->affected_rows();
				// var_dump($rows_affected);
				// return;
				if(!$results)
				{
					$data = "Upload failure. Error while loading excel data to database";
					echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
					return;
				}
				// Delete All the blank values from table
				$query = "delete from tmpcollstud where fcollcode = '' or fdegree = '' or fname = '' ";
				$results = $aobj_context->mobj_db->Execute($query);
				// For second sheet 
				if($i > 0)
				{
					// Check for the data in second sheet
					$query = "select count(*) as fcnt from tmpcollstud 
					where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
					
					$results = $aobj_context->mobj_db->GetRow($query);
					
					if($results['fcnt'] == 0)
					{
						break;
					}
				}


				$query = "select distinct fdegree from tmpcollstud 
				where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T' 
				and fdegree in (select distinct fdegree from degree)";
						
				$results = $aobj_context->mobj_db->GetAll($query);
						
				if(count($results) == 0)
				{
					$query = "delete from tmpcollstud where ifnull(fimported,'') <> 'T' and fcollcode = '{$collcode}'";
					// var_dump($query);
					$results = $aobj_context->mobj_db->GetAll($query);

					$data = "Upload failure. Invalid degree codes";
					echo $aobj_context->mobj_output->ToJSONEnvelope($data,-1,"failure"); 
					return;
				}

				$query = "update tmpcollstud set fimported = 'F' where ifnull(fimported,'') = ''";
				$results = $aobj_context->mobj_db->Execute($query);

				// insert records with no mobile number to collstudnb
				$query = "insert into collstudnb(fslno, fcollcode, fdegree, fmobileno, femail, fname) 
				select fslno, fcollcode, fdegree, fmobileno, femail, fname 
				from tmpcollstud where replace(ifnull(tmpcollstud.fmobileno,''),' ','') = '' 
				and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);
				// delete records in temp table with no mobilenumber
				$query = "delete from tmpcollstud where replace(ifnull(tmpcollstud.fmobileno,''),' ','') = '' 
				and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);

				// check for duplicates in the file uploading
				$query = "insert into tmpcollstuddup 
				select fmobileno, count(fmobileno) as fcnt from tmpcollstud 
				where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T' 
				group by fmobileno having count(fmobileno) > 1";
				$results = $aobj_context->mobj_db->Execute($query);

				// identify duplicates in file uploading
				$query = "update tmpcollstud a, tmpcollstuddup b set a.fdeleted = 'X' 
				where a.fmobileno = b.fmobileno and a.fcollcode = '{$collcode}' 
				and ifnull(a.fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);

				// insert duplicate records of that college to collstuddup
				$query = "insert into collstuddup 
				select * from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
				and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);
				// delete duplicates in temp table
				
				$query = "delete from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
				and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);
				
				// identify dulicates with previous inserted records (other than currently uploading college)
				$query = "update tmpcollstud, collstud set tmpcollstud.fdeleted = 'X' 
				where collstud.fmobileno = tmpcollstud.fmobileno and collstud.fcollcode <> tmpcollstud.fcollcode 
				and replace(ifnull(tmpcollstud.fmobileno,''),' ','') <> '' 
				and tmpcollstud.fcollcode = '{$collcode}' and ifnull(tmpcollstud.fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);
				// insert duplicates to collstuddup
				$query = "insert into collstuddup select * from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);

				// identify duplicate records (overall)
				$query = "update tmpcollstud, collstud set tmpcollstud.fdeleted = 'X' 
				where collstud.fmobileno = tmpcollstud.fmobileno 
				and replace(ifnull(tmpcollstud.fmobileno,''),' ','') <> '' and tmpcollstud.fcollcode = '{$collcode}' 
				and ifnull(tmpcollstud.fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);
				
				// delete duplicate records in temp table
				$query = "delete from tmpcollstud where fdeleted = 'X' and fcollcode = '{$collcode}' 
				and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);

				// insert remaining records to collstud
				$query = "insert into collstud(fslno, fcollcode, fdegree, fmobileno, femail, fname, fimpdate) 
				select fslno, fcollcode, fdegree, fmobileno, femail, fname, fimpdate from tmpcollstud 
				where fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);
				// Update records as imported 
				$query = "update tmpcollstud set fimported = 'T' where 
				fcollcode = '{$collcode}' and ifnull(fimported,'') <> 'T'";
				$results = $aobj_context->mobj_db->Execute($query);
				$i++;
			}
			$data = "Uploaded Successfully.";
			echo $aobj_context->mobj_output->ToJSONEnvelope($data,0,"success"); 
			return;	
		}
	}
	
}

?>