<?php




$cookie_name = "user_con";
	
if(!isset($_COOKIE[$cookie_name])) {
   // echo "Cookie named '" . $cookie_name . "' is not set!";
	header('Location: sign-in.php');
	exit;
} else {
    //echo "Cookie '" . $cookie_name . "' is set!<br>";
    //echo "Value is: " . $_COOKIE[$cookie_name];
}

Function RandomString()
	{
			$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
			$randstring = '';

			 for ($i = 0; $i < 12; $i++)
			 {
					$randVal =  rand (0,61);
					$randstring = $randstring . $characters[$randVal];
			 }

			return $randstring;
	}


//contractor_docs
$pid = $_GET['pid'];
$x = $_GET['x'];
$con_id =  $_COOKIE[$cookie_name];

$KEY	=  RandomString();
$KEY = strtoupper($KEY);


$allowedExts = array("gif", "jpeg", "jpg", "png","pdf","doc","docx","xls","xlsx", "txt","GIF", "JPEG", "JPG", "PNG","PDF","DOC","DOCX","XLS","XLSX", "TXT");
$temp = explode(".", $_FILES["fileToUpload"]["name"]);
$extension = end($temp);

$filname =  mysql_real_escape_string(basename($_FILES["fileToUpload"]["name"]));

echo $filname;
$withoutExt = preg_replace('/\\.[^.\\s]{3,4}$/', '', $filname);
 

$filname = $withoutExt . "_" . $KEY . "." . $extension;

echo $filname;
 

if (in_array($extension, $allowedExts))
 {
	if ($_FILES["fileToUpload"]["error"] > 0)
    {
      echo "Return Code: " . $_FILES["file"]["error"] . "<br>";
    }
	else
    { 

		echo "in";

		$folder = " Main "; //str_pad($pid, 6, "0", STR_PAD_LEFT);
 
 
		$con = mysql_connect('localhost', 'root', 'Boom95z') or die(mysql_error());
		mysql_select_db('orion9', $con);
 
		$result = mysql_query("select doc_title from contractor_documents where doc_title='" . $filname . "'");
 
 
		$cnt = mysql_num_rows($result);
						
		if ($cnt == 0)				
		{
			//insert new record also
			move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], "contractor_docs" .   "/" . $filname);
			
			mysql_query("insert into contractor_documents(con_id,doc_title,date_uploaded) values (" . $con_id . ",'" . $filname . "',now())");
			
		}	 
		else
		{
			echo 'Error: The project already contains a document with this name';
			exit;
		}
 
		mysql_close($con); 
	}
 }
 else
	 echo 'Error';
 
 
 //echo '<br/>dmah';
header('Location:contractor-documents.php');

?>
 
