PageRenderTime 71ms CodeModel.GetById 28ms RepoModel.GetById 0ms app.codeStats 1ms

/testes/upload.php

https://github.com/andrerezende/CENSO
PHP | 245 lines | 127 code | 85 blank | 33 comment | 40 complexity | bba02eb439e9ec11b5b398cb53473610 MD5 | raw file
  1. <?php
  2. include 'Pessoa.php';
  3. //var_dump()
  4. $arquivo1 = (isset($_FILES['arquivo']) ? $_FILES['arquivo'] : null);
  5. //$arquivo2 = (isset($_FILES['arquivo2']) ? $_FILES['arquivo2'] : null);
  6. //var_dump($arquivo1, $arquivo2);exit;
  7. if($arquivo1) {
  8. //var_dump($_FILES['arquivo']['type']);exit;
  9. $fp= fopen($arquivo1['tmp_name'], "r");
  10. //$fp2= fopen($arquivo2['tmp_name'], "r");
  11. //$registros_arq1 = null;
  12. //$registros_arq2 = null;
  13. //$i= 0;
  14. //printf('daniel lalalalal lalala la la');exit;
  15. //$estado = Array('"cod_estado"', '"sgl_estado"', "nom_estado");
  16. //$cidade = Array('"cod_cidade"', '"cod_estado"', '"nom_cidade"');
  17. $msg = 'o rato roeu a roupa do rei';
  18. //$qtd = substr_count($msg, 'roeu a roupa');
  19. $msg2 = str_replace("rato roeu", "cachorro", $msg);
  20. //var_dump($msg2);exit;
  21. while(!feof($fp)) {
  22. $ln = fgets($fp); // pega por linha
  23. if(Substr_count($ln, 'INDEX') == 0) { // se nao achar esta string dentro de ln faça
  24. $string = $ln;
  25. // estado
  26. $string = str_replace("INTEGER UNSIGNED NOT NULL AUTO_INCREMENT", 'SERIAL', $string);
  27. $string = str_replace('UNSIGNED', '', $string);
  28. $string = str_replace('login', 'usuario', $string);
  29. printf($string);echo '<BR/>';
  30. }
  31. }
  32. fclose($fp);
  33. exit;
  34. unset($string);
  35. while(!feof($fp2)) {
  36. $string = fgets($fp2); // pega por linha
  37. $registros_arq2[] = explode('%', $string);
  38. }
  39. fclose($fp1);
  40. fclose($fp2);
  41. // arrumando os dados
  42. $i = 0;
  43. foreach($registros_arq1 as $reg_unit) {
  44. //var_dump($reg_unit[1]);
  45. if(($reg_unit[0])) {
  46. $siape = str_replace (".", "", $reg_unit[0]);
  47. $siape = str_replace ("-", "", $siape);
  48. $registro_arq1[$i]['siape'] = trim($siape);
  49. }
  50. if(($reg_unit[1]))
  51. $registro_arq1[$i]['cpf'] = trim($reg_unit[1]);
  52. if(($reg_unit[2]))
  53. $registro_arq1[$i]['nome'] = trim($reg_unit[2]);
  54. $i++;
  55. }
  56. //var_dump($registro_arq1);exit;
  57. $i = 0;
  58. foreach($registros_arq2 as $reg_unit) {
  59. //var_dump($reg_unit[1]);
  60. if($reg_unit[1])
  61. $registro_arq2[$i]['siape'] = trim($reg_unit[1]);
  62. if(($reg_unit[2]))
  63. $registro_arq2[$i]['nome'] = $reg_unit[2];
  64. if(($reg_unit[3]) && ($reg_unit[4]) && ($reg_unit[5]))
  65. $registro_arq2[$i]['datanascimento'] = $reg_unit[5]."/".$reg_unit[4]."/".$reg_unit[3];
  66. if(($reg_unit[6]))
  67. $registro_arq2[$i]['sexo'] = ($reg_unit[6] == "M") ? "MASCULINO" : "FEMININO";
  68. if(($reg_unit[15]))
  69. $registro_arq2[$i]['endresidencial'] = $reg_unit[15];
  70. if(($reg_unit[16]))
  71. $registro_arq2[$i]['bairro'] = $reg_unit[16];
  72. if(($reg_unit[17]))
  73. $registro_arq2[$i]['cidade'] = $reg_unit[17];
  74. if(($reg_unit[18]))
  75. $registro_arq2[$i]['cep'] = $reg_unit[18];
  76. if(($reg_unit[20]))
  77. $registro_arq2[$i]['nacionalidade'] = $reg_unit[20];
  78. if(($reg_unit[22]) && ($reg_unit[21])) // 22 = ddd
  79. $registro_arq2[$i]['telefone'] = "(".$reg_unit[21].")". $reg_unit[22];
  80. if(($reg_unit[23]))
  81. $registro_arq2[$i]['email'] = $reg_unit[23];
  82. if(($reg_unit[24]))
  83. $registro_arq2[$i]['sangue'] = $reg_unit[24];
  84. if(($reg_unit[25])) {
  85. if($reg_unit[25] == "+") {
  86. $registro_arq2[$i]['fatorrh'] = "POSITIVO";
  87. } else {
  88. $registro_arq2[$i]['fatorrh'] = "NEGATIVO";
  89. }
  90. }
  91. $i++;
  92. }
  93. // cruzando as informaçoes.
  94. // unir os registros do arquivo 1 ao arquivo 2 usando o numero da matricula do arquivo 1
  95. // $registro_arq1 tem ID, CPF E NOME
  96. // $registro_arq1 tem as demais informaçoes
  97. // procurando a matricula de $i em cada $j
  98. for($i = 0; $i < count($registro_arq1); $i++) {
  99. for($j = 0; $j < count($registro_arq2); $j++) {
  100. //var_dump($registro_arq1[$i]);//exit;
  101. if(isset($registro_arq1[$i]['siape']) && isset($registro_arq2[$j]['siape'])) {
  102. //var_dump($registro_arq1[$i]['siape'], $registro_arq2[$j]['siape']); echo '<BR><BR>';
  103. if(Substr_count($registro_arq1[$i]['siape'], $registro_arq2[$j]['siape']) != 0) { // procurando a string do registro_arq1 em registro_arq2
  104. //echo '1';
  105. $registro_arq2[$j]['cpf'] = $registro_arq1[$i]['cpf'];
  106. $registro_arq2[$j]['siape'] = $registro_arq1[$i]['siape']; // o siape do arq1 é mais completo.
  107. $registro_completo[] = $registro_arq2[$j];
  108. break;
  109. } else {
  110. //echo '0';
  111. }
  112. } else {
  113. //echo 'nao setado';
  114. }
  115. }
  116. }
  117. if(isset($registro_completo)) {
  118. // Tratando o valor de cada campo e colocando uma aspas simples antes de enviar pra classe conexao.
  119. $x = 0;
  120. foreach($registro_completo as $registro) {
  121. foreach($registro as $campoFromPOST=>$valor) {
  122. $registro_completo[$x][$campoFromPOST] = "'".Pessoa::anti_injection($valor)."'";
  123. }
  124. $x++;
  125. }
  126. //var_dump($registro_completo);exit;
  127. $status = Pessoa::importaParaBanco($registro_completo);
  128. }
  129. //var_dump($status);exit;
  130. $qtd_registros_adicionados = $status[0];
  131. $qtd_registros_falhados = $status[1];
  132. //$erro = $status['erro'];
  133. if($qtd_registros_adicionados > 0) {
  134. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
  135. <script>alert(\"$qtd_registros_adicionados registros foram importados com sucesso!\");
  136. </script>";
  137. if($qtd_registros_falhados > 0) {
  138. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
  139. <script>alert(\"$qtd_registros_falhados registros possuem caracteres inválidos e não foram adicionados.\");
  140. history.go(-1);
  141. </script>";
  142. } else {
  143. echo "<script>
  144. history.go(-1);
  145. </script>";
  146. }
  147. } else {
  148. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
  149. <script>alert(\"Houve uma falha na importação dos registros.\");
  150. history.go(-1);
  151. </script>";
  152. }
  153. //var_dump($_FILES["arquivo"]["error"]);exit;
  154. } else {
  155. echo "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />
  156. <script>alert(\"Não acesse este arquivo diretamente!\");
  157. location.href = \"../formAutenticado.php\";
  158. </script>";
  159. }
  160. ?>