0
I created a script where the user will send one or more CSV files, it will send and then php should take the name of that file and make some changes (system requirement)and save in the database and delete that file, the problem is the following with 1 file it converts smoothly with 2 or more files it doesn’t 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" />';
}
?>
Is there an error? or does the request timeout occur?
– rray
foreach($arquivo as $arquivo)
gets complicated. Use 2 different names in the variables offoreach
.– Bacco
Keeps giving error even after changing the foreach Warning: fopen(28-11-2014_09-04-52.): failed to open stream: No such file or directory in
– Lucas