Carregando o arquivo...

"; $arqcsv = $_FILES['i3GEOuploadcsv']['tmp_name']; $nomePrefixo = str_replace(" ","_",removeAcentos($_FILES['i3GEOuploadcsv']['name'])); $nomePrefixo = $nomePrefixo."_".(nomeRandomico(4)); $Arquivo = $_FILES['i3GEOuploadcsv']['tmp_name']; $status = move_uploaded_file($Arquivo,$dir_tmp."/".$nomePrefixo); if($status != 1){ echo "

Ocorreu um erro no envio do arquivo";exit; } if(!file_exists($dir_tmp."/".$nomePrefixo)){ echo "

Ocorreu algum problema no envio do arquivo ".$dir_tmp."/".$nomePrefixo;paraAguarde();exit; } $arqcsv = $dir_tmp."/".$nomePrefixo; //pega os parametros de conexao include("classe_metaestat.php"); $m = new Metaestat(); $conexao = $m->listaConexao($_POST["i3GEOuploadcsvcodigoconexao"],true); //pega a lista de colunas e identifica o separador utilizado $handle = fopen ($arqcsv, "r"); $cabecalho = fgets($handle); $buffer = str_replace('"','',$cabecalho); $buffer = str_replace("'",'',$buffer); $buffer = str_replace("\n",'',$buffer); $buffer = str_replace("\r",'',$buffer); $colunas = explode(";",$buffer); $separador = ";"; if(count($colunas) == 1){ $colunas = explode(",",$buffer); $separador = ","; } //var_dump($colunas); fclose ($handle); //le o csv em um array $handle = fopen ($arqcsv, "r"); $linhas = array(); $ncolunas = count($colunas); while (!feof($handle)) { $buffer = fgets($handle); if($buffer != $cabecalho){ $buffer = str_replace('"','',$buffer); $buffer = str_replace("'",'',$buffer); $buffer = str_replace("\n",'',$buffer); $buffer = str_replace("\r",'',$buffer); $temp = explode($separador,$buffer); if(count($temp) == $ncolunas) $linhas[] = $temp; } } fclose ($handle); //decobre o tipo de coluna $testar = 10; if(count($linhas) < $testar){ $testar = count($linhas); } $tipoColuna = array(); for ($j=0; $j<$ncolunas;$j++){ $tipo = "numeric"; for ($i=0; $i<$testar;$i++){ $s = $linhas[$i]; $v = $s[$j]; if(!is_numeric($v)){ $tipo = "varchar"; } } $tipoColuna[$colunas[$j]] = $tipo; } echo "
Tipos das colunas:

";
			var_dump($tipoColuna);
			echo "
"; //gera o script para criar a tabela $sqltabela = array(); $sql = "CREATE TABLE ".$_POST["i3GEOuploadcsvesquema"].".".$_POST["tabelaDestinocsv"]."("; $temp = array(); foreach($colunas as $coluna){ $temp[] = strtolower($coluna)." ".$tipoColuna[$coluna]; } $sql .= implode(",",$temp).") WITH(OIDS=FALSE)"; $sqltabela[] = $sql; $sqltabela[] = "ALTER TABLE ".$_POST["i3GEOuploadcsvesquema"].".".$_POST["tabelaDestinocsv"]." OWNER TO ".$conexao["usuario"]; echo "
Sql tabela:
";
			var_dump($sqltabela);
			echo "
"; //gera o script para inserir os dados $linhasql = array(); $insert = "INSERT INTO ".$_POST["i3GEOuploadcsvesquema"].".".$_POST["tabelaDestinocsv"]."(".strtolower(implode(",",$colunas)).")"; $nlinhas = count($linhas); for ($i=0; $i<$nlinhas;$i++){ $s = $linhas[$i]; $vs = array(); for ($j=0; $j<$ncolunas;$j++){ if($tipoColuna[$colunas[$j]] == "varchar"){ $vs[] = "'".$s[$j]."'"; } else{ $vs[] = $s[$j]; } } $linhasql[] = $insert."VALUES(".implode(",",$vs).")"; } //echo "
";
			//var_dump($linhasql);exit;
			try {
				$dbh = new PDO('pgsql:dbname='.$conexao["bancodedados"].';user='.$conexao["usuario"].';password='.$conexao["senha"].';host='.$conexao["host"].';port='.$conexao["porta"]);
			} catch (PDOException $e) {
		echo 'Connection failed: ' . $e->getMessage();
	}

	foreach($sqltabela as $linha){
		try {
			$dbh->query($linha);
		} catch (PDOException $e) {
			echo 'Erro: ' . $e->getMessage();
		}
	}
	foreach($linhasql as $linha){
		try {
			$dbh->query($linha);
		} catch (PDOException $e) {
			echo 'Erro: ' . $e->getMessage();
		}
	}
	echo "
Feito!!!
Faça o reload da página"; } else{ echo "

Erro ao enviar o arquivo. Talvez o tamanho do arquivo seja maior do que o permitido.

"; } ?>