Example:
1 - Separated by semicolon.
Layout File:
FULANO 1;1999-01-01
FULANO 2;1999-02-02
FULANO 3;1999-03-03
FULANO 4;1999-04-04
Code for insertion of this 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
Layout File: (the name being 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 insertion of this 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>