0
How to do the function: explode(',', $linha);
import a . txt file separated by two dots in a . txt file instead of using the comma.
The file . txt is currently this
Cesar, [email protected], cesar, 123456
Kelly, [email protected], kelly, 123457
Jessica, [email protected], jessica, 123458
The idea would be to stay that way
Cesar:[email protected]:cesar:123456
Kelly:[email protected]:kelly:123457
Jessica:[email protected]:jessica:123458
Php code
//Incluir a conexao com BD
include_once("conexao.php");
//Receber os dados do formulário
$arquivo_tmp = $_FILES['arquivo']['tmp_name'];
//ler todo o arquivo para um array
$dados = file($arquivo_tmp);
//Ler os dados do array
foreach($dados as $linha){
//Retirar os espaços em branco no inicio e no final da string
$linha = trim($linha);
//Colocar em um array cada item separado pela virgula na string
$valor = explode(',', $linha);
//Recuperar o valor do array indicando qual posição do array requerido e atribuindo para um variável
$nome = $valor[0];
$email = $valor[1];
$usuario = $valor[2];
$senha = $valor[3];
//Criar a QUERY com PHP para inserir os dados no banco de dados
$result_usuario = "INSERT INTO usuarios (nome, email, usuario, senha) VALUES ('$nome', '$email', '$usuario', '$senha')";
//Executar a QUERY para inserir os registros no banco de dados com MySQLi
$resultado_usuario = mysqli_query($conn, $result_usuario);
}
//Criar a variável global com a mensagem de sucesso
$_SESSION['msg'] = "<p style='color: green;'>Carregado os dados com sucesso!</p>";
//Redirecionar o usuário com PHP para a página index.php
header("Location: index.php");
By uploading the file. txt to the server separating the name, email and password data with two dots and no spaces all information is only entered in the database name field.
Would you accept a response that uses only the shell command? (Not using php). My suggestion is to use
sed
.– Danizavtz
Barter
explode(',', $linha);
forexplode(':', $linha);
.– Benilson
@Benilson when changing to two points he imports everything in one column in the database as shown in the image of phpmyadmin.
– the flash
The file is with
:
or,
? Not clear. You want to change the txt file with,
for:
or he’s already like this?– TiagoA
@Tiagoa has two points. The problem is that with two points it imports all the data for only one column, when it should separate by name, email, user and password.
– the flash
I did a test here online and it worked with your code: https://repl.it/@tiagoa/txt-two-points
– TiagoA
@Tiagoa It really worked, thank you.
– the flash
the ideal would be to make a single INSERT outside the FOREACH.
– user60252
instead of creating the QUERY within the loop create the Insert VALUES
– user60252
$dataString .= " ('$name', '$email', '$user', '$password'),";
– user60252
out of the loop remove the last comma $values=substr($dataString, 0, -1);
– user60252
and do the Insert like this: $result_usuario = "INSERT INTO usuarios (name, email, user, password) VALUES $values";
– user60252
as stated earlier, make the Insert out of the loop
– user60252
@Leocaracciolo Thanks for the tips, I will add in the code.
– the flash