mark
mark

Reputation: 13

PHP read remote file through FTP connection

I have simple code to load data from txt file to mysql, it works perfect but my last one problem is that my file is on ftp server not in local folder. Below is my code how rewrite this line to connect with my file on ftp (test.txt) with using credentials?

$open=fopen('test.txt','r');

$conn = mysqli_connect('localhost','user','password','db_name');

if(!$conn)
{
    die(mysqli_error());
}

$query = mysqli_query($conn, "DELETE FROM test");

$open = fopen('test.txt','r');

fgets($open);
fgets($open);
while (!feof($open)) 
{
  $getTextLine = fgets($open);
  $explodeLine = explode("|",$getTextLine);

  list($Login,$Inday,$Start_przerwy,$Koniec_przerwy,$Czas_przerwy,$Odcinek) = $explodeLine;

  $qry = "insert into test (Login,Inday,Start_przerwy,Koniec_przerwy,Czas_przerwy,Odcinek) values('".$Login."','".$Inday."','".$Start_przerwy."','".$Koniec_przerwy."','".$Czas_przerwy."','".$Odcinek."')";
  mysqli_query($conn,$qry);
}
fclose($open);
echo "done";

Upvotes: 1

Views: 1426

Answers (1)

num8er
num8er

Reputation: 19372

Simply the same but define protocol ftp:// and username, password

$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$open = fopen($file, "r");

or:

$file = "ftp://username:pa‌​ssword@hostname/path/to/test.txt";
$lines = explode("\n", file_get_contents($file));

foreach($lines AS $line) {
  $line = trim($line);
  // body of while
}


no need for ftp_* methods, keep it simple (:

Upvotes: 1

Related Questions