How to read a file with a mailing list?

8

I have a text file with many emails and I want to save everything in a database, how do I get each email from the file in PHP?

Emails are comma-separated.

    
asked by anonymous 07.05.2014 / 01:04

2 answers

7

You can use file() to read the emails in> and put them in a array .

$texto = file('emails.txt');

Now just use explode() to capture the emails > and separate them with the comma.

$emails = explode(",", $texto);
    
07.05.2014 / 01:15
2

Starting from the presumption that the database has the e-mail table, and that the file is teste.txt :

// Pega o conteudo do arquivo teste.txt
$arquivo_texto = file_get_contents('teste.txt');

// Cria um array com os emails do arquivo teste.txt
$emails = explode(',', $arquivo_texto);

// Abre uma conexão com o banco de dados mysql
$con = new PDO("mysql:host=localhost;dbname=nome_do_banco", "root", "");

// Loop para inserção no banco de dados
foreach ($emails as $value) {
 $rs = $con->query("INSERT INTO emails VALUES ?");
 $rs->bindParam(1, $value);
 $rs->execute();
}
    
28.05.2014 / 19:22