<?php
// define constants
/*define('SOURCE_DIR', '../match-documents/');
define('THUMBS_DIR', '../match-documents/thumbs/');
define('MAX_WIDTH', 320);
define('MAX_HEIGHT', 278);*/
// process the uploaded document
if (is_uploaded_file($_FILES['document']['tmp_name'][$i])) {
	$original = $_FILES['document']['tmp_name'][$i];

	/* // begin by getting the details of the original
	list($width, $height, $type) = getdocumentsize($original);
	// calculate the scaling ratio
	if ($width <= MAX_WIDTH && $height <= MAX_HEIGHT) {
	$ratio = 1;
	}
	elseif ($width > $height) {
	$ratio = MAX_WIDTH/$width;
	}
	else {
	$ratio = MAX_HEIGHT/$height;
	} */
	// strip the extension off the document filename
	/* $documenttypes = array('/\.gif$/', '/\.jpg$/', '/\.jpeg$/', '/\.png$/');
	$name = preg_replace($documenttypes, '', basename($original)); */
	$_FILES['document']['name'][$i]= str_replace(" ","_",$_FILES['document']['name'][$i]);
	// move the temporary file to the upload folder
	if(!file_exists(UPLOAD_DIR.$_FILES['document']['name'][$i])){
	$moved = @ move_uploaded_file($original, 
	UPLOAD_DIR.$_FILES['document']['name'][$i]);
	if ($moved) {
	$result = $_FILES['document']['name'][$i].' successfully uploaded; ';
	$original = UPLOAD_DIR.$_FILES['document']['name'][$i];

	}
	else {
	$result = 'Problem uploading '.$_FILES['document']['name'][$i].'; ';
	}
	}
	else{
		//echo "<strong>document already exists in the database. Please upload a new one</strong>
	//			<a href='my_imgupload_both.php'>back to upload form </a>
	//		";
	//		exit();
		ini_set('date.timezone','Europe/London');
		$now = date('d-m-Y-His');
		$moved = move_uploaded_file($original, UPLOAD_DIR.$now.$_FILES['document']['name'][$i]);
		if ($moved) {
			$result = $_FILES['document']['name'][$i].' successfully uploaded; ';
			$original = UPLOAD_DIR.$now.$_FILES['document']['name'][$i];
			$file = $now.$_FILES['document']['name'][$i];
		}
	}
	

	$insertSQL = sprintf("INSERT INTO `documents` (categoryID, doctitle, document, dcomment, extension) VALUES (%s, %s, %s, %s, %s)",
							GetSQLValueString($_POST['categoryID'], "text"),
							GetSQLValueString($_POST['doctitle'], "text"),
							GetSQLValueString($file, "text"),
							GetSQLValueString($_POST['dcomment'], "text"),
							GetSQLValueString($_POST['extension'], "text")
						);


	$Result1 = mysqli_query($conn_ama, $insertSQL) or die(mysqli_error($conn_ama));
		if($Result1){
			$result = "datasent";
			}
			else{
				$result = "error";
			}

	}
?>