Read 2 or more csv files

0

I created a script where the user will send one or more CSV file, it will send and then php should get the name of that file and make some changes (system requirement) and save in the database and delete that file, problem is as follows with 1 file it quietly converts with 2 or more files it does not work anymore

HTML

<html lang="pt-br">
<head>
    <title>Arquivo CSV</title>
    <meta charset="utf-8" />
</head>
<body>
    <div id="wrapper">
        <center><h1>Exportar Excel</h1></center>
        <form method="post" name="frm_csv" action="ler_csv.php" enctype="multipart/form-data"  >
            <center><fieldset style="width:50px">
                <legend>Script CSV</legend>
                <label for="arquivo">Arquivo CSV: 
                    <input id="file_csv" name="file_csv[]" type="file"  required><br><br>

                </label>
                <label for "ano">Ano:    
                    <input id="ano" name="ano" type="text" size="4" required><br><br>
                </label>
                <label for "ano">Semestre:    
                    <input id="semestre" name="semestre" type="text" size="1" required><br><br>
                </label>

                <button type="submit">OK</button>
            </fieldset></center>
        </form>
    </div>
</body>
</html>

PHP

<?
require_once 'conecta_pg.inc';
require_once 'includes.inc';

$ano=$_REQUEST['ano'];
$semestre=$_REQUEST['semestre'];

$arquivo = isset($_FILES["file_csv"]) ? $_FILES["file_csv"] : FALSE;

foreach($arquivo as $arquivo){
$arquivo_nome_disciplina = $arquivo["name"];
preg_match("/\.(csv){1}$/i", $arquivo["name"], $ext);


$arquivo_nome = date("d-m-Y_H-i-s") . "." . $ext[1];
move_uploaded_file($arquivo["tmp_name"], $arquivo_nome);

$row = 0;
$handle = fopen ($arquivo_nome,"r");


$nome_disciplina = explode("_", $arquivo_nome_disciplina);
//echo $nome_disciplina[0];
//echo str_replace(".csv","",$nome_disciplina[1]);
$nome_disciplina[1]=str_replace(".csv","",$nome_disciplina[1]);


$db = conecta_banco();
$comando="insert into avaliacao.disciplina(nome,codigo) values('".$nome_disciplina[1]."','".$nome_disciplina[0]."')";
$sql= pg_query($db,$comando);



while ($data = fgetcsv ($handle, 1000, ";")) {
   $num = count ($data);
   $row++;

   $coluna1 = $data[0];


   if($coluna1!='RA'){

        $aluno = localiza_aluno_ra($coluna1);
        //echo "RA:".$coluna1."RA da Tabela".$aluno['ra']; //CONFERE SE ESTÁ TRAZENDO OS RA CORRETOS
        $disciplina = localiza_disciplina($nome_disciplina[1]);
        //echo "Nome Disciplina: ".$nome_disciplina[0]."Nome Disciplina Tabela:".$disciplina['codigo']; //CONFERE SE ESTÁ TRAZENDO AS DISCIPLINAS CORRETAMENTE


            if($aluno['id']){   
              if($disciplina['id']){
                $db = conecta_banco();
                $comando2="insert into avaliacao.aluno_disciplina(id_aluno,id_disciplina,ano,semestre) values (".$aluno['id'].",".$disciplina['id'].",".$ano.",".$semestre.")";
                $sql=pg_query($db,$comando2) or die ("<center><font size=20>Registro já foi inserido</font><center>");
              }     
            }   

    }
}
fclose ($handle);

unlink($arquivo_nome);
}
if($sql){
    echo "<center><font size=20>Arquivo salvo</font><center>";
    echo '<meta http-equiv="refresh" content="3;URL=index.php" />';
} 
?>
    
asked by anonymous 27.11.2014 / 18:18

0 answers