<?php
error_reporting(E_ALL);
ini_set('display_errors', 0 );
header('Content-Type: text/html; charset=utf-8');
include("../configuracao.php");//confirma no banco de dados
include("../inclusos/funcoes.php");
include("../seguranca.php"); // Inclui o arquivo com o sistema de seguran?a
protegePagina(); // Protege a página

$origem = $_GET['origem'];
$idcliente = $_GET['idcliente'];
$idfornecedor = $_GET['idfornecedor'];
$idusuario = $_GET['idusuario'];
$idvenda = $_GET['idvenda'];
$idconta = $_GET['idconta'];
$idpagamento = $_GET['idpagamento'];
$idmovimento = $_GET['idmovimento'];
$idcompra_prima = $_GET['idcompra_prima'];
$idcompra = $_GET['idcompra'];
$idobra = $_GET['idobra'];
$idservicovenda = $_GET['idservicovenda'];
$idservicoobra = $_GET['idservicoobra'];
$idpasta = $_GET['idpasta'];
$idsubpasta = $_GET['idsubpasta'];


if(count($_FILES['attachment']['name']) > 0){
    		
		// File Error Checking 
		foreach($_FILES['attachment']['error'] as $error) {
			if($error)
				die( "Error: ".$error); 		
		}

		/*		
		// File size Resctrictions
		$maxsize = 2 * 1024 * 1024; //2MB maximum allowed.
		foreach($_FILES['attachment']['size'] as $size) {
			if($size > $maxsize)
				die("Error: File size is larger than the allowed limit.");		
		}
		*/	
		// File Type Restrictions
/*		$allowed = array(".doc", ".txt", ".pdf", ".docx", ".jpg",".jpeg", ".png", ".jpeg", ".xls", ".xlsx", ".gif", ".zip", ".dwg");
		foreach($_FILES['attachment']['name'] as $name) {
			$type = pathinfo($name, PATHINFO_EXTENSION); 
			if(!in_array($type, $allowed)) 
				die("Error: Please select a valid file format.-".$type);	
		}
*/	
		
		
		$Kv_items =  array();	
		$Kv = 0;
		$uploads_dir = "uploads/";
		foreach($_FILES['attachment']['name'] as $filename) {
			move_uploaded_file($_FILES["attachment"]["tmp_name"][$Kv], $uploads_dir. basename($filename)); 
			mysql_query($kv, "INSERT INTO arquivos (idcliente, idfornecedor, idusuario, idvenda, idconta, idpagamento, idmovimento, idcompra_prima, idcompra, idobra, titulo, arquivo, datacadastro, usuario_cadastro, origem, idservicovenda, idservicoobra, idpasta, idsubpasta) values ( '".$_GET['idcliente']."', '".$_GET['idfornecedor']."', '".$_GET['idusuario']."', '".$_GET['idvenda']."', '".$_GET['idconta']."', '".$_GET['idpagamento']."', '".$_GET['idmovimento']."', '".$_GET['idcompra_prima']."', '".$_GET['idcompra']."','".$_GET['idobra']."', '".$_FILES["attachment"]["name"][$Kv]."', '".$_FILES["attachment"]["name"][$Kv]."', '".date('Y-m-d')."', '".$_SESSION['usuarioID']."', '".$_GET['origem']."', '$idservicovenda', '$idservicoobra', '$idpasta', '$idsubpasta')", $conexao);
			if(mysql_insert_id($kv)){
				$Kv++;
				$Kv_items[] = mysql_insert_id($kv);  
			}					
		} 
		if(count($Kv_items))
			echo count($Kv_items).' Files Inserted Successfully!' ; 
} 

$kv->close();

?>