Example:
1 - Separated by point and comma.
File layout:
FULANO 1;1999-01-01
FULANO 2;1999-02-02
FULANO 3;1999-03-03
FULANO 4;1999-04-04
Code for entering such data
<?php
function Inserir($itens, Pdo $pdo){
$sts = $pdo->prepare("INSERT INTO dados(nome, data) VALUES(?,?);");
$sts->bindValue(1, $itens[0], PDO::PARAM_STR);
$sts->bindValue(2, $itens[1], PDO::PARAM_STR);
$sts->execute();
$sts->closeCursor();
$sts = NULL;
}
if (!empty($_FILES['arquivo']))
{
$Pdo = new PDO("mysql:host=localhost;dbname=testdb", "root", "senha");
$file = fopen($_FILES['arquivo']['tmp_name'], 'r');
while (!feof($file)){
$linha = fgets($file);
$itens = explode(';', $linha);
Inserir($itens, $Pdo);
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Arquivo</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" method="post">
<input type="file" name="arquivo" id="arquivo">
<input type="submit" name="enviar" value="Enviar">
</form>
</body>
</html>
2 - Separated by spaces
File Layout: (where the name comprises 60 spaces (0.60) and the date plus 10 (60.70)
FULANO 1 1999-01-01
FULANO 2 1999-02-02
FULANO 3 1999-03-03
FULANO 4 1999-04-04
Code for entering such data
<?php
function Inserir($itens, Pdo $pdo){
$sts = $pdo->prepare("INSERT INTO dados(nome, data) VALUES(?,?);");
$sts->bindValue(1, $itens[0], PDO::PARAM_STR);
$sts->bindValue(2, $itens[1], PDO::PARAM_STR);
$sts->execute();
$sts->closeCursor();
$sts = NULL;
}
if (!empty($_FILES['arquivo']))
{
$Pdo = new PDO("mysql:host=localhost;dbname=testdb", "root", "senha");
$file = fopen($_FILES['arquivo']['tmp_name'], 'r');
while (!feof($file)){
$linha = fgets($file);
$itens = array(trim(substr($linha, 0, 60)), trim(substr($linha, 60, 70)));
Inserir($itens, $Pdo);
}
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Arquivo</title>
</head>
<body>
<form action="<?php echo $_SERVER['PHP_SELF'];?>" enctype="multipart/form-data" method="post">
<input type="file" name="arquivo" id="arquivo">
<input type="submit" name="enviar" value="Enviar">
</form>
</body>
</html>
ditto
– brasofilo
@Brasofilo suggest you transcribe the comment there, because it took me a long time to realize that it was link, and that you were not simply agreeing with the previous comment (that was deleted). I liked the comment there, but I only saw it because it was the only one left. (<Joke>comments that contains only links can become obsolete if the original answer is deleted. To learn more read....</Joke> :P ) . then delete this one.
– Bacco